Reputation: 3103
I am trying to assign a variable within eval
, and then print it out. The code is:
eval "foo=nil"
puts foo
What I get is:
undefined local variable or method `foo' for main:Object (NameError)
When I use puts
within the eval
, I get no errors. This means that foo
is scoped within the eval
. How can I get it to be outside the eval
scope, yet not as a global variable?
Upvotes: 0
Views: 82
Reputation: 4396
I think foo is going out of scope in your eval.
if you declare it before your eval it will work ok. Eg;
foo='foo'
eval "foo=nil"
puts foo.to_s
output:
=> nil
Upvotes: 2