From: Florian Gross Date: 2004-12-09T09:37:30+09:00 Subject: Re: binding question email55555 email55555 wrote: > I know it is ugly. But: > > (1) Why it works on irb and not on "ruby pgm.rb" ? IRB wraps everything you do in eval(). It also does a bunch of other magic to make everything work correctly in common cases. > (2) What is the correct way to "dynamically" create local variable ? > For example: how to create local variables from an given local > vairable name strings array ? The problem is that Ruby decides on compile time whether 'a' refers to the variable 'a' (if it has seen an assignment to it earlier) or whether it is the method call 'a()'. A solution would be to set the variable to nil before you use it, but I think it is generally a bad idea to assign to variables of your caller. Are you sure you can not use a Hash instead? Also note that methods in Ruby can trivially return multiple values like this: def plus_minus(a, b) return(a + b, a - b) end sum, sub = plus_minus(1, 3) sum # => 4 sub # => -2