Eval and Later Defined Local Variables
I've noticed the following behavior with eval:
def example eval("foo") # => nil, but would expect a NameError eval("bar") # => NameError foo = 1 end
I'm assuming this behavior is expected, since "eval" executes in the context of the existing YARV instructions and local table, but I'm not certain since I haven't seen this behavior documented anywhere.
Updated by jeremyevans0 (Jeremy Evans) 1 day ago
ko1 (Koichi Sasada) wrote:
This is specification.
examplemethod and define
evalmethod, compile given string (
"foo") and defined
foowas used (without any problem).
So it is documentation issue.
Can anyone write about it?
Attached is a documentation patch. However, a committer with more knowledge of the internals should probably check to see if it is technically accurate.
Updated by chrisseaton (Chris Seaton) 1 day ago
Updated by nobu (Nobuyoshi Nakada) 1 day ago
jeremyevans0 (Jeremy Evans) wrote:
Attached is a documentation patch.
However, local variables set inside the call to +eval+ will not be reflected in the surrounding scope.
Isn't "set" vague a little, between "defined" an "assigned"?
Updated by jeremyevans0 (Jeremy Evans) about 15 hours ago
nobu (Nobuyoshi Nakada) wrote:
"local variables assigned inside the call to +eval+" sounds like a just assignment to a local variable defined outside, doesn't it?
It could definitely be interpreted that way. I'll reword it to make it more clear. Thanks!