ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Eregon (Benoit Daloze) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "Eregon (Benoit Daloze)" <noreply@ruby-lang.org>
Subject: [ruby-core:120122] [Ruby master Bug#20930] Different semantics for nested `it` and `_1`
Date: Fri, 06 Dec 2024 18:14:27 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-110873.20241206181427.772@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20930.20241204211824.772@ruby-lang.org>

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/

  parent reply	other threads:[~2024-12-06 18:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-04 21:18 [ruby-core:120104] " 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 ` Eregon (Benoit Daloze) via ruby-core [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-110873.20241206181427.772@ruby-lang.org \
    --to=ruby-core@ml.ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).