From: milouse <noreply@ruby-lang.org>
To: ruby-core@neon.ruby-lang.org
Subject: [ruby-core:110747] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
Date: Mon, 14 Nov 2022 09:52:50 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-100084.20221114095250.26731@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16796.20200417082329.26731@ruby-lang.org
Issue #16796 has been updated by milouse (Étienne Deparis).
shyouhei (Shyouhei Urabe) wrote in #note-3:
> sawa (Tsuyoshi Sawada) wrote in #note-2:
>
> That's too confusing. The following code works today.
>
> ```ruby
> case "str"
> in /s(.)r/ => match_data
> match_data[1] # => "t"
> end
> ```
Not really (or at least it is not what I understand from the original request, as I felt on a similar issue today). You are just pointing out an edge case where `match_data[1]` will be `t`, not because it is actually a `MatchData` object, which first capturing group worth `t`, but just because the pattern matching feature select this branch and put the original `String` "str" onto `match_data`. Thus `match_data[1]` here only gives you the second char of the string `match_data`.
The previous example would have fail (in my understanding of the problem), with the following regexp:
```ruby
case "str"
in /(.)tr/ => match_data
match_data[1] # => "t" instead of the expected "s"
end
```
By the way, there is in fact a very simple way to achieve the initial goal (again, from my understanding of the problem):
```
case "str"
when /s(?<mid>.)r/
match_data = Regexp.last_match
p match_data[:mid]
end
```
----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796#change-100084
* Author: UlyssesZhan (Ulysses Zhan)
* Status: Rejected
* Priority: Normal
----------------------------------------
I want to use
```ruby
case "str"
when /s(?<mid>.)r/
p mid
end
```
instead of
```ruby
case
when /s(?<mid>.)r/ =~ "str"
p mid
end
```
I also do not like using `$1`.
This feature is extremely useful when there are a lot of `when`s.
--
https://bugs.ruby-lang.org/
prev parent reply other threads:[~2022-11-14 9:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-17 8:23 [ruby-core:97935] [Ruby master Feature#16796] Assigning local variables when using `case when regexp` UlyssesZhan
2020-04-17 8:53 ` [ruby-core:97936] " nobu
2020-04-17 10:20 ` [ruby-core:97939] " sawadatsuyoshi
2020-04-17 11:11 ` [ruby-core:97940] " shyouhei
2020-04-17 16:38 ` [ruby-core:97946] " shevegen
2020-04-18 12:48 ` [ruby-core:97949] " matz
2022-11-14 9:52 ` milouse [this message]
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-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-100084.20221114095250.26731@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
--cc=ruby-core@neon.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 read-only IMAP folder(s) and NNTP newsgroup(s).