From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@...> Date: 2024-12-05T00:13:58+00:00 Subject: [ruby-core:120110] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Issue #20930 has been updated by mame (Yusuke Endoh). Good catch. I see two problems. One is an incompatibility with Prism's handling of `_1`. I think it should be handled as an error like parse.y. Especially when parsed with the 3.3 version's syntax, there is no other choice but an error: ```ruby Prism.parse("[1].each { _1; [2].each { _1 } }", version: "3.3.0") ``` The other problem is how to interpret `it`. I think Ruby master's it behavior is good. I have experienced mistakes of a name conflict issue of a local variable in a different scope, but not as often. However, just adding a read from `it` outside of a block only changes the meaning of inner `it`, which I think would increase the frequency of mistakes very much. ```ruby [1].each { [2].each { p it }} #=> 1 [1].each { it; [2].each { p it }} #=> 2 (!) ``` ---------------------------------------- Bug #20930: Different semantics for nested `it` and `_1` https://bugs.ruby-lang.org/issues/20930#change-110858 * Author: Eregon (Benoit Daloze) * Status: Open * ruby -v: ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) +PRISM [x86_64-linux] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- ``` $ ruby -ve '[1].each { p it; [5].each { p it } }' ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) +PRISM [x86_64-linux] 1 5 $ ruby -ve '[1].each { p _1; [5].each { p _1 } }' ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) +PRISM [x86_64-linux] 1 1 ``` Notice the inconsistency, `it` uses the innermost block, `_1` uses the outermost block. I think `_1` semantics are slightly better, at least `_1` behaves like a normal local variable declared in the outer block then. Note that on 3.3.5 it was forbidden to nest `_1` which I think might be good for clarity/avoiding ambiguity: ``` $ ruby -ve '[1].each { p _1; [5].each { p _1 } }' ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux] -e:1: numbered parameter is already used in -e:1: outer block here [1].each { p _1; [5].each { p _1 } } ruby: compile error (SyntaxError) ``` --- As an aside, mixing `_1` and `it` is allowed, I think this is [good](https://bsky.app/profile/eregon.bsky.social/post/3lcg4fjcf7225), they are different things so there is not much confusion there: ``` $ ruby -ve '[1].each { p _1; [5].each { p it } }' ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) +PRISM [x86_64-linux] 1 5 $ ruby -ve '[1].each { p it; [5].each { p _1 } }' ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) +PRISM [x86_64-linux] 1 5 ``` -- 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/