Project

General

Profile

Actions

Bug #13337

closed

Eval and Later Defined Local Variables

Added by faraz (Faraz Yashar) about 7 years ago. Updated almost 5 years ago.

Status:
Closed
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

Updated by ko1 (Koichi Sasada) almost 7 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) almost 5 years 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) almost 5 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"?

Actions #5

Updated by jeremyevans (Jeremy Evans) almost 5 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) almost 5 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) almost 5 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!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0