Project

General

Profile

Actions

Bug #20965

closed

`it` vs `binding.local_variables`

Added by zverok (Victor Shepelev) about 18 hours ago. Updated about 5 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-12-15T13:36:38Z master 366fd9642f) +PRISM [x86_64-linux]
[ruby-core:120303]

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.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #18980: `it` as a default block parameterClosedk0kubun (Takashi Kokubun)Actions
Actions #1

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
# => []
Actions #4

Updated by k0kubun (Takashi Kokubun) about 7 hours ago

Actions #5

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like2Like0Like0Like0