From: "mame (Yusuke Endoh) via ruby-core" Date: 2024-12-23T03:37:39+00:00 Subject: [ruby-core:120369] [Ruby master Bug#20965] `it` vs `binding.local_variables` Issue #20965 has been updated by mame (Yusuke Endoh). Status changed from Closed to Open Let's revert this change. Below is our local discussion of this issue with @nobu and @ko1. First, consider the following example. ```ruby "foo".tap do it "bar".tap do p eval("it") # what should happen? end end ``` There are three possible options. 1. Raises an exeption 2. Returns `"bar"` 3. Returns `"foo"` 1 is the only realistic choice. 2 would require keeping all arguments conservatively even when `it` does not appear lexically, which we want to avoid for a performance reason (including the possibility of future optimizations, as @ko1 said). 3 is clearly counterintuitive. Then, consider the following example. ```ruby "foo".tap do it "bar".tap do p binding.local_variables #=> []? [:it]? eval("it") end end ``` If `local_variables` contains `:it`, `eval("it")` is expected to return some value. However, it is impossible for the reason above. Therefore, `local_variables` should return `[]`. The difference between `_1` and `it` is that `_1` is prohibited to be referenced outside of a block, while `it` is not. Note that if `it` is defined as an ordinary local variable by an assignment, `local_variables` should contain `:it`. ---------------------------------------- Bug #20965: `it` vs `binding.local_variables` https://bugs.ruby-lang.org/issues/20965#change-111145 * Author: zverok (Victor Shepelev) * Status: Open * ruby -v: ruby 3.4.0dev (2024-12-15T13:36:38Z master 366fd9642f) +PRISM [x86_64-linux] * Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONTNEED ---------------------------------------- `it` is not available in the list of `binding.local_varaibles`, **unlike** numbered parameters: ```ruby 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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/