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/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>