ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
@ 2024-12-04 21:18 Eregon (Benoit Daloze) via ruby-core
  2024-12-04 21:26 ` [ruby-core:120105] " Eregon (Benoit Daloze) via ruby-core
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-12-04 21:18 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20930 has been reported by Eregon (Benoit Daloze).

----------------------------------------
Bug #20930: Different semantics for nested `it` and `_1`
https://bugs.ruby-lang.org/issues/20930

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:120105] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
  2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
@ 2024-12-04 21:26 ` Eregon (Benoit Daloze) via ruby-core
  2024-12-05  0:13 ` [ruby-core:120110] " mame (Yusuke Endoh) via ruby-core
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-12-04 21:26 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20930 has been updated by Eregon (Benoit Daloze).


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

Which is still inconsistent between `it` and `_1`.

----------------------------------------
Bug #20930: Different semantics for nested `it` and `_1`
https://bugs.ruby-lang.org/issues/20930#change-110852

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:120110] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
  2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
  2024-12-04 21:26 ` [ruby-core:120105] " Eregon (Benoit Daloze) via ruby-core
@ 2024-12-05  0:13 ` mame (Yusuke Endoh) via ruby-core
  2024-12-05 17:44 ` [ruby-core:120113] " k0kubun (Takashi Kokubun) via ruby-core
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2024-12-05  0:13 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:120113] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
  2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
  2024-12-04 21:26 ` [ruby-core:120105] " Eregon (Benoit Daloze) via ruby-core
  2024-12-05  0:13 ` [ruby-core:120110] " mame (Yusuke Endoh) via ruby-core
@ 2024-12-05 17:44 ` k0kubun (Takashi Kokubun) via ruby-core
  2024-12-06 18:14 ` [ruby-core:120122] " Eregon (Benoit Daloze) via ruby-core
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: k0kubun (Takashi Kokubun) via ruby-core @ 2024-12-05 17:44 UTC (permalink / raw)
  To: ruby-core; +Cc: k0kubun (Takashi Kokubun)

Issue #20930 has been updated by k0kubun (Takashi Kokubun).


> I think _1 semantics are slightly better, at least _1 behaves like a normal local variable declared in the outer block then.

Yeah but `_1` isn't declared in the outer block (or anywhere), so `_1` doesn't necessarily need to behave like a local variable declared in the outer block. You could also say "_1 should behave like a normal local variable declared in the inner block", and it seems as plausible as what you said.

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

I agree with @mame 's opinion.

When you have nested loops, you would use the iterator of the inner-most block most often. The current behavior of `it` seems to have more use cases than the one of `_1`. Ruby has prioritized solving real-world use cases over just making existing features consistent, so I don't think `it` needs to be consistent with `_1` here.

----------------------------------------
Bug #20930: Different semantics for nested `it` and `_1`
https://bugs.ruby-lang.org/issues/20930#change-110861

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:120122] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
  2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
                   ` (2 preceding siblings ...)
  2024-12-05 17:44 ` [ruby-core:120113] " k0kubun (Takashi Kokubun) via ruby-core
@ 2024-12-06 18:14 ` Eregon (Benoit Daloze) via ruby-core
  2024-12-09 20:30 ` [ruby-core:120142] " k0kubun (Takashi Kokubun) via ruby-core
  2024-12-12 10:47 ` [ruby-core:120207] " mame (Yusuke Endoh) via ruby-core
  5 siblings, 0 replies; 7+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-12-06 18:14 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20930 has been updated by Eregon (Benoit Daloze).


I think it's OK for `it` to always use the innermost block, but it should be a conscious choice and ideally documented (maybe even part of `NEWS`).
If we go there, I do think we should use the same semantics for `_1`, `_2`, etc for consistency, because they are extremely similar constructs. Otherwise `it` would become more powerful than `_1` but they should really just be equivalent.

----------------------------------------
Bug #20930: Different semantics for nested `it` and `_1`
https://bugs.ruby-lang.org/issues/20930#change-110873

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:120142] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
  2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
                   ` (3 preceding siblings ...)
  2024-12-06 18:14 ` [ruby-core:120122] " Eregon (Benoit Daloze) via ruby-core
@ 2024-12-09 20:30 ` k0kubun (Takashi Kokubun) via ruby-core
  2024-12-12 10:47 ` [ruby-core:120207] " mame (Yusuke Endoh) via ruby-core
  5 siblings, 0 replies; 7+ messages in thread
From: k0kubun (Takashi Kokubun) via ruby-core @ 2024-12-09 20:30 UTC (permalink / raw)
  To: ruby-core; +Cc: k0kubun (Takashi Kokubun)

Issue #20930 has been updated by k0kubun (Takashi Kokubun).

Description updated

Let me get this straight. parse.y was the default parser when `_1` and `it` were implemented, so Prism should have copied it. @alanwu has filed https://github.com/ruby/prism/issues/3291 for it, so let's not talk about it here. I updated your issue description to clarify what's really the problem here.

As to nested `_1` being a `SyntaxError` and nested `it` being allowed, it is a conscious choice (for me) because it seems useful to be able to nest blocks like `files.map { JSON.parse(it).each { puts it } }` and it doesn't seem confusing to me. I'll put this into the dev-meeting agenda.

----------------------------------------
Bug #20930: Different semantics for nested `it` and `_1`
https://bugs.ruby-lang.org/issues/20930#change-110894

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



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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:120207] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
  2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
                   ` (4 preceding siblings ...)
  2024-12-09 20:30 ` [ruby-core:120142] " k0kubun (Takashi Kokubun) via ruby-core
@ 2024-12-12 10:47 ` mame (Yusuke Endoh) via ruby-core
  5 siblings, 0 replies; 7+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2024-12-12 10:47 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

Issue #20930 has been updated by mame (Yusuke Endoh).


Discussed at the dev meeting, and matz confirmed that the current master's behavior is good.

----------------------------------------
Bug #20930: Different semantics for nested `it` and `_1`
https://bugs.ruby-lang.org/issues/20930#change-110970

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

The behavior 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>



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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-12-12 10:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-04 21:18 [ruby-core:120104] [Ruby master Bug#20930] Different semantics for nested `it` and `_1` Eregon (Benoit Daloze) via ruby-core
2024-12-04 21:26 ` [ruby-core:120105] " Eregon (Benoit Daloze) via ruby-core
2024-12-05  0:13 ` [ruby-core:120110] " mame (Yusuke Endoh) via ruby-core
2024-12-05 17:44 ` [ruby-core:120113] " k0kubun (Takashi Kokubun) via ruby-core
2024-12-06 18:14 ` [ruby-core:120122] " Eregon (Benoit Daloze) via ruby-core
2024-12-09 20:30 ` [ruby-core:120142] " k0kubun (Takashi Kokubun) via ruby-core
2024-12-12 10:47 ` [ruby-core:120207] " mame (Yusuke Endoh) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).