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" ??
>
>  
>

Because ruby determines at compile time if `x' should be treated as a 
method call or as a variable lookup.
If previously in the code there was an assignment to `x', then ruby 
considers it a variable. But at compile time it can not eval the string, 
so it thinks that `x' is a method.