Project

General

Profile

Bug #20930

Updated by k0kubun (Takashi Kokubun) 9 days ago

With --parser=parse.y: 

 ``` 
 $ ruby --parser=parse.y -ve '[1].each { p it; [5].each { p it } }' 
 ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) [x86_64-linux] 
 1 
 5 
 $ ruby --parser=parse.y -ve '[1].each { p _1; [5].each { p _1 } }' 
 ruby 3.4.0dev (2024-12-04T19:29:24Z master 3c91a1e5fd) [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) 
 ``` 

 It is inconsistent between it and _1. 

 --- 

 <details> 
 <summary>Side note about mixing `_1` and `it`, which seems good</summary> 

 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 
 ``` 

 </details> 

 --- 

 <details> 
 <summary>Prism's bug, moved to: https://github.com/ruby/prism/issues/3291</summary> 

 ``` 
 $ 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) 
 ``` 

 </details> --- 

 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 
 ```

Back