ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:97935] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
@ 2020-04-17  8:23 UlyssesZhan
  2020-04-17  8:53 ` [ruby-core:97936] " nobu
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: UlyssesZhan @ 2020-04-17  8:23 UTC (permalink / raw)
  To: ruby-core

Issue #16796 has been reported by UlyssesZhan (有丘 詹).

----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796

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

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

* [ruby-core:97936] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
  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 ` nobu
  2020-04-17 10:20 ` [ruby-core:97939] " sawadatsuyoshi
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: nobu @ 2020-04-17  8:53 UTC (permalink / raw)
  To: ruby-core

Issue #16796 has been updated by nobu (Nobuyoshi Nakada).


It is because `when` calls `===` method but not `=~`.

----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796#change-85160

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

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

* [ruby-core:97939] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
  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 ` sawadatsuyoshi
  2020-04-17 11:11 ` [ruby-core:97940] " shyouhei
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sawadatsuyoshi @ 2020-04-17 10:20 UTC (permalink / raw)
  To: ruby-core

Issue #16796 has been updated by sawa (Tsuyoshi Sawada).


I have met such use cases, and agree this would be useful.

Alternatively, in order to access even unnamed matches, assigning the last matched data to a variable with the following syntax may be useful.

```ruby
case "str"
when /s(?<mid>.)r/ => match_data
  match_data[:mid] # => "t"
end
```

```ruby
case "str"
when /s(.)r/ => match_data
  match_data[1] # => "t"
end
```


----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796#change-85162

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

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

* [ruby-core:97940] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
  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 ` shyouhei
  2020-04-17 16:38 ` [ruby-core:97946] " shevegen
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: shyouhei @ 2020-04-17 11:11 UTC (permalink / raw)
  To: ruby-core

Issue #16796 has been updated by shyouhei (Shyouhei Urabe).


sawa (Tsuyoshi Sawada) wrote in #note-2:
> Alternatively, in order to access even unnamed matches, assigning the last matched data to a variable with the following syntax may be useful.
> 
> ```ruby
> case "str"
> when /s(.)r/ => match_data
>   match_data[1] # => "t"
> end
> ```

That's too confusing.  The following code works today.

```ruby
case "str"
in /s(.)r/ => match_data
  match_data[1] # => "t"
end
```

----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796#change-85163

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

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

* [ruby-core:97946] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
  2020-04-17  8:23 [ruby-core:97935] [Ruby master Feature#16796] Assigning local variables when using `case when regexp` UlyssesZhan
                   ` (2 preceding siblings ...)
  2020-04-17 11:11 ` [ruby-core:97940] " shyouhei
@ 2020-04-17 16:38 ` shevegen
  2020-04-18 12:48 ` [ruby-core:97949] " matz
  2022-11-14  9:52 ` [ruby-core:110747] " milouse
  5 siblings, 0 replies; 7+ messages in thread
From: shevegen @ 2020-04-17 16:38 UTC (permalink / raw)
  To: ruby-core

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/

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

* [ruby-core:97949] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
  2020-04-17  8:23 [ruby-core:97935] [Ruby master Feature#16796] Assigning local variables when using `case when regexp` UlyssesZhan
                   ` (3 preceding siblings ...)
  2020-04-17 16:38 ` [ruby-core:97946] " shevegen
@ 2020-04-18 12:48 ` matz
  2022-11-14  9:52 ` [ruby-core:110747] " milouse
  5 siblings, 0 replies; 7+ messages in thread
From: matz @ 2020-04-18 12:48 UTC (permalink / raw)
  To: ruby-core

Issue #16796 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

Implicit assignment from regular expression matches has been a bad idea. I shouldn't have merged it at the first moment.
I'd like to remove it altogether if I have a chance. So I don't like to enhance it any further.

Matz.


----------------------------------------
Feature #16796: Assigning local variables when using `case when regexp`
https://bugs.ruby-lang.org/issues/16796#change-85173

* Author: UlyssesZhan (有丘 詹)
* 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/

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

* [ruby-core:110747] [Ruby master Feature#16796] Assigning local variables when using `case when regexp`
  2020-04-17  8:23 [ruby-core:97935] [Ruby master Feature#16796] Assigning local variables when using `case when regexp` UlyssesZhan
                   ` (4 preceding siblings ...)
  2020-04-18 12:48 ` [ruby-core:97949] " matz
@ 2022-11-14  9:52 ` milouse
  5 siblings, 0 replies; 7+ messages in thread
From: milouse @ 2022-11-14  9:52 UTC (permalink / raw)
  To: ruby-core

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/

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

end of thread, other threads:[~2022-11-14  9:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [ruby-core:110747] " milouse

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