From: ts Date: 2002-06-05T15:23:02+09:00 Subject: Re: eval and local variable >>>>> "P" == Park Heesob writes: P> eval "foo = 'bar'" P> p local_variables #--> ["foo"] P> p eval "foo" #--> "bar" P> p foo #--> foo.rb:4: undefined local variable or method `foo' for P> # (NameError) P> Why can't use foo variable directly? When ruby compile the script, it find `foo' at this time there is no local variable defined (eval is not yet executed) and ruby think that you have wanted to write `foo()', i.e. a call to the method #foo At runtime, eval will create a local variable, but when ruby execute the lines `p foo' it will try to call the method #foo, because this is what it has compiled. Here an example pigeon% cat b.rb #!/usr/bin/ruby def foo "ruby has called #foo" end eval "foo = 'bar'" p local_variables p eval "foo" p foo pigeon% pigeon% b.rb ["foo"] "bar" "ruby has called #foo" pigeon% Guy Decoux