ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:95332] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution
       [not found] <redmine.issue-16251.20191015131939@ruby-lang.org>
@ 2019-10-15 13:19 ` teslur
  2019-10-15 13:48 ` [ruby-core:95333] " mame
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: teslur @ 2019-10-15 13:19 UTC (permalink / raw)
  To: ruby-core

Issue #16251 has been reported by teslur (Tetsushi FUKABORI).

----------------------------------------
Bug #16251: Evaluation in binding differs from ruby execution
https://bugs.ruby-lang.org/issues/16251

* Author: teslur (Tetsushi FUKABORI)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
In specific situation, I found that result of string evaluation in Binding returns different from ruby execution result.

In following sample code, ruby evaluates `method_or_local_var` as method call and returns `"method"`.
However, `binding.eval` evaluates `method_or_local_var` as local variable and returns `nil`.

Here is sample code.
```
def method_or_local_var
  'method'
end

if true
  puts "execute method_or_local_var:"
  p method_or_local_var #=> "method"

  puts "execute method_or_local_var by bind.eval('method_or_local_var'):"
  p binding.eval('method_or_local_var') #=> nil
else
  method_or_local_var = 'local variable'
end
```

and here is results of execute sample code.
```
❯ ruby sample_code.rb
execute method_or_local_var:
"method"
execute method_or_local_var by bind.eval('method_or_local_var'):
nil
```

I expect evaluation result of `method_or_local_var` in binding to be method, and returns `"method"`.
Is this the expected behavior?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:95333] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution
       [not found] <redmine.issue-16251.20191015131939@ruby-lang.org>
  2019-10-15 13:19 ` [ruby-core:95332] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution teslur
@ 2019-10-15 13:48 ` mame
  2019-10-15 20:27 ` [ruby-core:95347] " eregontp
  2019-10-24  2:37 ` [ruby-core:95525] " xkernigh
  3 siblings, 0 replies; 4+ messages in thread
From: mame @ 2019-10-15 13:48 UTC (permalink / raw)
  To: ruby-core

Issue #16251 has been updated by mame (Yusuke Endoh).


I have no idea whether this is a spec or undefined behavior, but the current implementation is actually intentional.

```
def x
  "x"
end

def foo
  eval("x = 1") # foo has no variable named x, so 1 is assigned to a temporal variable
  p eval("x") #=> "x" # there is no variable named x in this scope, so it is regarded as a method call
end
foo

def bar
  eval("x = 1") # foo has a variable named x, so 1 is assigned to the variable
  p eval("x") #=> 1 # foo has a variable named x, so it is regarded as a variable reference
  x = nil
end
bar
```

IMO, you'd better not to depend upon the behavior in any way.

----------------------------------------
Bug #16251: Evaluation in binding differs from ruby execution
https://bugs.ruby-lang.org/issues/16251#change-82041

* Author: teslur (Tetsushi FUKABORI)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
In specific situation, I found that result of string evaluation in Binding returns different from ruby execution result.

In following sample code, ruby evaluates `method_or_local_var` as method call and returns `"method"`.
However, `binding.eval` evaluates `method_or_local_var` as local variable and returns `nil`.

Here is sample code.
```
def method_or_local_var
  'method'
end

if true
  puts "execute method_or_local_var:"
  p method_or_local_var #=> "method"

  puts "execute method_or_local_var by bind.eval('method_or_local_var'):"
  p binding.eval('method_or_local_var') #=> nil
else
  method_or_local_var = 'local variable'
end
```

and here is results of execute sample code.
```
❯ ruby sample_code.rb
execute method_or_local_var:
"method"
execute method_or_local_var by bind.eval('method_or_local_var'):
nil
```

I expect evaluation result of `method_or_local_var` in binding to be method, and returns `"method"`.
Is this the expected behavior?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:95347] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution
       [not found] <redmine.issue-16251.20191015131939@ruby-lang.org>
  2019-10-15 13:19 ` [ruby-core:95332] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution teslur
  2019-10-15 13:48 ` [ruby-core:95333] " mame
@ 2019-10-15 20:27 ` eregontp
  2019-10-24  2:37 ` [ruby-core:95525] " xkernigh
  3 siblings, 0 replies; 4+ messages in thread
From: eregontp @ 2019-10-15 20:27 UTC (permalink / raw)
  To: ruby-core

Issue #16251 has been updated by Eregon (Benoit Daloze).


Local variables are "hoisted" to the beginning of the method/block in Ruby.
With that in mind, I think the behavior is logical. IMHO, it is spec.

----------------------------------------
Bug #16251: Evaluation in binding differs from ruby execution
https://bugs.ruby-lang.org/issues/16251#change-82056

* Author: teslur (Tetsushi FUKABORI)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
In specific situation, I found that result of string evaluation in Binding returns different from ruby execution result.

In following sample code, ruby evaluates `method_or_local_var` as method call and returns `"method"`.
However, `binding.eval` evaluates `method_or_local_var` as local variable and returns `nil`.

Here is sample code.
```
def method_or_local_var
  'method'
end

if true
  puts "execute method_or_local_var:"
  p method_or_local_var #=> "method"

  puts "execute method_or_local_var by bind.eval('method_or_local_var'):"
  p binding.eval('method_or_local_var') #=> nil
else
  method_or_local_var = 'local variable'
end
```

and here is results of execute sample code.
```
❯ ruby sample_code.rb
execute method_or_local_var:
"method"
execute method_or_local_var by bind.eval('method_or_local_var'):
nil
```

I expect evaluation result of `method_or_local_var` in binding to be method, and returns `"method"`.
Is this the expected behavior?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:95525] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution
       [not found] <redmine.issue-16251.20191015131939@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-10-15 20:27 ` [ruby-core:95347] " eregontp
@ 2019-10-24  2:37 ` xkernigh
  3 siblings, 0 replies; 4+ messages in thread
From: xkernigh @ 2019-10-24  2:37 UTC (permalink / raw)
  To: ruby-core

Issue #16251 has been updated by kernigh (George Koehler).


This is a simpler example of the behavior:

```
$ ruby -e 'p x; x = 6'
Traceback (most recent call last):
-e:1:in `<main>': undefined local variable or method `x' for main:Object (NameError)
$ ruby -e 'p eval("x"); x = 6'
nil
$ ruby -e 'p binding.eval("x"); x = 6'
nil
```

A plain `x` raises NameError, but an `eval("x")` or `binding.eval("x")` fetches nil from the local variable x.  I expect this behavior, because Ruby parses a script before running it:

1. Ruby parses `x = 6` and adds x to the binding.
2. Ruby runs `eval("x")` with x in the binding.
3. Ruby runs `x = 6` and changes the value of x from nil to 6.

Binding#local_variables reveals the local variable named x:

```
$ ruby -e 'p binding.eval("x"); x = 2'
[:x]
```

Because x exists, `binding.eval("x")` has the expected behavior, so there is no bug.

----------------------------------------
Bug #16251: Evaluation in binding differs from ruby execution
https://bugs.ruby-lang.org/issues/16251#change-82300

* Author: teslur (Tetsushi FUKABORI)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
In specific situation, I found that result of string evaluation in Binding returns different from ruby execution result.

In following sample code, ruby evaluates `method_or_local_var` as method call and returns `"method"`.
However, `binding.eval` evaluates `method_or_local_var` as local variable and returns `nil`.

Here is sample code.
```
def method_or_local_var
  'method'
end

if true
  puts "execute method_or_local_var:"
  p method_or_local_var #=> "method"

  puts "execute method_or_local_var by bind.eval('method_or_local_var'):"
  p binding.eval('method_or_local_var') #=> nil
else
  method_or_local_var = 'local variable'
end
```

and here is results of execute sample code.
```
❯ ruby sample_code.rb
execute method_or_local_var:
"method"
execute method_or_local_var by bind.eval('method_or_local_var'):
nil
```

I expect evaluation result of `method_or_local_var` in binding to be method, and returns `"method"`.
Is this the expected behavior?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-10-24  2:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-16251.20191015131939@ruby-lang.org>
2019-10-15 13:19 ` [ruby-core:95332] [Ruby master Bug#16251] Evaluation in binding differs from ruby execution teslur
2019-10-15 13:48 ` [ruby-core:95333] " mame
2019-10-15 20:27 ` [ruby-core:95347] " eregontp
2019-10-24  2:37 ` [ruby-core:95525] " xkernigh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).