Bug #20965
closed`it` vs `binding.local_variables`
Description
it
is not available in the list of binding.local_varaibles
, unlike numbered parameters:
p(proc { binding.local_variables }.call) # []
p(proc { |x| binding.local_variables }.call) # [:x]
p(proc { _1; binding.local_variables }.call) # [:_1]
p(proc { vars = binding.local_variables; _1; vars }.call) # [:_1, :vars]
p(proc { it; binding.local_variables }.call) # []
I wonder if it is deliberate or accidental.
Updated by zverok (Victor Shepelev) about 18 hours ago
- ruby -v set to ruby 3.4.0dev (2024-12-15T13:36:38Z master 366fd9642f) +PRISM [x86_64-linux]
Updated by shan (Shannon Skipper) about 13 hours ago
It does seem like that last one should be [:it]
for consistency if it wasn't intentional. +1
An aside, but it's also interesting that using _2
causes _1
to also be defined under Binding#local_variables.
proc { _9; local_variables }.call
# => [:_1, :_2, :_3, :_4, :_5, :_6, :_7, :_8, :_9]
proc { it; local_variables }.call
# => []
Updated by nobu (Nobuyoshi Nakada) about 8 hours ago
Updated by k0kubun (Takashi Kokubun) about 7 hours ago
- Related to Feature #18980: `it` as a default block parameter added
Updated by nobu (Nobuyoshi Nakada) about 7 hours ago
- Status changed from Open to Closed
Applied in changeset git|46fec0f62a1803d44edb8b06e39ac0f358e56670.
[Bug #20965] Define it
like an ordinary argument (#12398)
Also fixes [Bug #20955]
Updated by tompng (tomoya ishida) about 5 hours ago
I have a concern of making it
parameter an lvar.
1| 42.tap do
2| p binding.local_variable_get('it')
3| it /1/i
4| p it
5| end
When parser reads p it
at line 4, it
turns out to be a local variable, I think from the beginning of the block(from line 2).
But on line 3, it /1/i
is already parsed as if lvar it
does not exist.
It is impossible to re-parse the block again, so the original implementation(it parameter is not an lvar) makes sense to me.