Eric Hodel
12/8/2004 9:52:00 PM
On 08 Dec 2004, at 13:44, email55555 email55555 wrote:
> I have this example codes:
>
> b = binding
> ['x', 'y'].each { |e| eval("#{e} = 123", b) }
> p local_variables
> p x
> p y
>
> Run the codes from irb, everything works.
> However, save it to a file like pgm.rb and ruby it as "ruby pgm.rb", I
> got the error message as:
> ["b", "x", "y"] #==> it does print the local_variables correctely.
> pgm.rb:4: undefined local variable or method 'x' for main:Object
> (NameError)
>
> Why? local_variables array does show me the "x", why it fail on "p x"
> ??
"name =" creates a local variable in Ruby, and you have defined no
method 'x'. They may also exist in the binding, but Ruby doesn't allow
you to get at them that way.
eval("#{e} = 123", binding) looks ugly, so you should expect strange
things to happen.