Bug #13337
closedEval and Later Defined Local Variables
Description
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.
Files
Updated by ko1 (Koichi Sasada) over 8 years ago
This is specification.
(1) parse example method and define foo.
(2) In eval method, compile given string ("foo") and defined foo was used (without any problem).
So it is documentation issue.
Can anyone write about it?
Updated by jeremyevans0 (Jeremy Evans) over 6 years ago
- File doc-eval-local-var.patch doc-eval-local-var.patch added
ko1 (Koichi Sasada) wrote:
This is specification.
(1) parse
examplemethod and definefoo.
(2) Inevalmethod, compile given string ("foo") and definedfoowas 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) over 6 years ago
Updated by nobu (Nobuyoshi Nakada) over 6 years ago
jeremyevans0 (Jeremy Evans) wrote:
Attached is a documentation patch.
Seems fine.
A question:
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 jeremyevans (Jeremy Evans) over 6 years ago
- Status changed from Open to Closed
Applied in changeset git|0bd5f846df2f6268f653773e0cd4a20e2a944646.
Document local variable interactions with eval
Fixes [Bug #13337]
Updated by nobu (Nobuyoshi Nakada) over 6 years ago
"local variables assigned inside the call to +eval+" sounds like a just assignment to a local variable defined outside, doesn't it?
Updated by jeremyevans0 (Jeremy Evans) over 6 years 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!