[ruby-core:96895] [Ruby master Feature#16432] Using `_1` inside `binding.irb` will cause unintended behavior
From:
ko1@...
Date:
2020-01-16 07:27:01 UTC
List:
ruby-core #96895
Issue #16432 has been updated by ko1 (Koichi Sasada).
* in short:
```ruby
1.times{
p _1 #=> 0
eval("[:a, :b].each{p _1}")
#=> 0
#=> 0
}
```
mame: it can cause confusing bug.
ko1: we have two solutions:
1. separate numbered parameter scope between in/out eval.
* Issue: we can't see `_1` variables from debugging reason.
* allow `Binding#local_variable_get("_1")` for this purpose? How to get uplevel binding?
2. Same semantics without `eval`.
* issue: we can't write numbered parameters in this kind of context.
```ruby
# 2.7.0 behavior
1.times{p _1
eval('p _1') #=> OK (0)
eval('[:a].each{p _1}') #=> OK, but confusing (0)
}
# 1.
1.times{p _1
eval('p _1') #=> NameError
eval('[:a].each{p _1}') #=> OK (:a)
}
# 2.
1.times{p _1
eval('p _1') #=> OK (0)
eval('[:a].each{p _1}') #=> SyntaxError
}
```
```ruby
1.times{binding.irb}
# start IRB session
irb> p _1
#=>
# 2.7.0 NameError
# 1. NameError
# 2. NameError
1.times{binding.irb;_1}
# start IRB session
irb> p _1
#=>
# 2.7.0 OK (0)
# 1. NameError
# 2. OK (0)
irb> [:a].each{p _1}
#=>
# 2.7.0 OK, but confusing (0)
# 1. OK (:a)
# 2. SyntaxError
```
matz: vote for 1
Conclusion:
* Solution 1. Nobu will fix parse.y
* hopefully backport to 2.7.1 (March 2020)
----------------------------------------
Feature #16432: Using `_1` inside `binding.irb` will cause unintended behavior
https://bugs.ruby-lang.org/issues/16432#change-83909
* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
## Summary
Calling `binding.irb` in a block that uses `_1` and using `_1` in `irb` will cause unintended behavior.
## Steps to reproduce
1. Use `_1` in block
2. Use `binding.irb` in block
3. Use block with `_1` in `binding.irb`
```ruby
# test.rb
proc {
binding.irb
_1
}.call 42
```
```ruby
# binding.irb
irb> (1..10).map { _1 + _1 }
```
## Expected behavior
```ruby
irb> _1
=> 42
irb> (1..10).map { _1 + _1 }
= > [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```
## Actual behavior
* Refers to the `_1` of the block that called` binding.irb`
```ruby
irb> _1
=> 42
irb> (1..10).map { _1 + _1 }
= > [84, 84, 84, 84, 84, 84, 84, 84, 84, 84]
```
I think this is an unintended behavior for the user.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>