From: shevegen@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:97946] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
Date: Fri, 17 Apr 2020 16:38:45 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-85168.20200417163845.26731@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16796.20200417082329.26731@ruby-lang.org
Issue #16796 has been updated by shevegen (Robert A. Heiler).
sawa wrote:
> I have met such use cases, and agree this would be useful.
I don't disagree in the sense that there may be valid use cases,
but the syntax is kind of weird:
/s(?<mid>.)r/
I use regexes a lot of course, since they are useful. But do we
really gain a lot from making what used to be simple, harder?
I understand that you may avoid an extra step (assignment, after
the regex match), but to me personally I find that style so
much harder to read, compared to:
if /(foo.+)/
mid = $1.to_s.dup # or something like that
end
Admittedly I am very much a very oldschool-ruby person. ;)
By the way, while I personally do not really like $ variables,
I actually use them a LOT, whereas I rarely use match_data[]
syntax style. Dunno why, perhaps a habit, but the $1 $2 etc..
are ony of the few (semi) global variables that I like.
(I write "semi" because they tend to be more volatile, which
is why I may tend to use .dup like a semi-crazy person a
lot, rather than fix a regex or handle nils - I .to_s.dup
all the things! ;) )
----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796#change-85168
* Author: UlyssesZhan (有丘 詹)
* Status: Open
* 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/
next prev parent reply other threads:[~2020-04-17 16:38 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 ` shevegen [this message]
2020-04-18 12:48 ` [ruby-core:97949] " matz
2022-11-14 9:52 ` [ruby-core:110747] " milouse
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-85168.20200417163845.26731@ruby-lang.org \
--to=ruby-core@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).