Actions
Feature #16432
closedUsing `_1` inside `binding.irb` will cause unintended behavior
Feature #16432:
Using `_1` inside `binding.irb` will cause unintended behavior
Status:
Closed
Assignee:
-
Target version:
-
Description
Summary¶
Calling binding.irb in a block that uses _1 and using _1 in irb will cause unintended behavior.
Steps to reproduce¶
- Use
_1in block - Use
binding.irbin block - Use block with
_1inbinding.irb
Expected behavior¶
Actual behavior¶
- Refers to the
_1of the block that calledbinding.irb
I think this is an unintended behavior for the user.
Updated by nobu (Nobuyoshi Nakada) over 6 years ago
Updated by osyo (manga osyo) over 6 years ago
Thanks comments :)
This is because the eval code is executed at the current context exactly.
Another choice is to raise a syntax error at the runtime.
Yes, and if _1 is nested, a syntax error will occur.
If _1 is nested, I think it is better to runtime error rather than unintended behavior.
Updated by ko1 (Koichi Sasada) over 6 years ago
- in short:
mame: it can cause confusing bug.
ko1: we have two solutions:
- separate numbered parameter scope between in/out eval.
- Issue: we can't see
_1variables from debugging reason.- allow
Binding#local_variable_get("_1")for this purpose? How to get uplevel binding?
- allow
- Same semantics without
eval.
- issue: we can't write numbered parameters in this kind of context.
# 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
}
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)
Updated by nobu (Nobuyoshi Nakada) over 6 years ago
- Status changed from Open to Closed
Applied in changeset git|c171ab23e376b6c7f1094a77f137d916b0a403e6.
Separate numbered parameter scope in eval
[Feature #16432]
Updated by nobu (Nobuyoshi Nakada) over 4 years ago
- Related to Bug #18558: Numbered arguments + eval added
Actions