Project

General

Profile

Bug #13337

Eval and Later Defined Local Variables

Added by faraz (Faraz Yashar) over 2 years ago. Updated about 15 hours ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:80253]

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

doc-eval-local-var.patch (1.63 KB) doc-eval-local-var.patch jeremyevans0 (Jeremy Evans), 06/24/2019 08:56 PM

Associated revisions

Revision 0bd5f846
Added by jeremyevans (Jeremy Evans) about 16 hours ago

Document local variable interactions with eval

Fixes [Bug #13337]

History

Updated by ko1 (Koichi Sasada) about 2 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) 1 day ago

ko1 (Koichi Sasada) wrote:

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?

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 nobu (Nobuyoshi Nakada) 1 day 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"?

#5

Updated by jeremyevans (Jeremy Evans) about 16 hours 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) about 15 hours 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) 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!

Also available in: Atom PDF