[#114703] [Ruby master Bug#19875] Ruby 2.7 -> 3.1 Performance regression in String#count — "iz (Illia Zub) via ruby-core" <ruby-core@...>

Issue #19875 has been reported by iz (Illia Zub).

18 messages 2023/09/12

[#114774] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes — "p8 (Petrik de Heus) via ruby-core" <ruby-core@...>

Issue #19884 has been reported by p8 (Petrik de Heus).

13 messages 2023/09/15

[#114796] [Ruby master Feature#19889] Let `Kernel.#require` search for files relative to the current working directory for non ./, ../ relative paths — "sawa (Tsuyoshi Sawada) via ruby-core" <ruby-core@...>

Issue #19889 has been reported by sawa (Tsuyoshi Sawada).

6 messages 2023/09/18

[#114803] [Ruby master Bug#19890] File#realine(chomp: true) slower/more allocations than readline.chomp! — "segiddins (Samuel Giddins) via ruby-core" <ruby-core@...>

Issue #19890 has been reported by segiddins (Samuel Giddins).

12 messages 2023/09/18

[#114817] [Ruby master Bug#19892] Build failure with 8f1b688177 — "vo.x (Vit Ondruch) via ruby-core" <ruby-core@...>

Issue #19892 has been reported by vo.x (Vit Ondruch).

8 messages 2023/09/19

[#114915] [Ruby master Feature#19905] Introduce `Queue#peek` — "hi@... (Joao Fernandes) via ruby-core" <ruby-core@...>

Issue #19905 has been reported by hi@joaofernandes.me (Joao Fernandes).

8 messages 2023/09/28

[ruby-core:114688] [Ruby master Feature#19840] [Proposal] Expand Find pattern to Multiple Find

From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@...>
Date: 2023-09-12 07:35:56 UTC
List: ruby-core #114688
Issue #19840 has been updated by mame (Yusuke Endoh).

Assignee set to ktsj (Kazuki Tsujimoto)

----------------------------------------
Feature #19840: [Proposal] Expand Find pattern to Multiple Find
https://bugs.ruby-lang.org/issues/19840#change-104518

* Author: FlickGradley (Nick Bradley)
* Status: Open
* Priority: Normal
* Assignee: ktsj (Kazuki Tsujimoto)
----------------------------------------
Hello! I love Ruby's pattern matching features. I would like to propose an expansion of the Find pattern which allows the selection of multiple matching elements of an array.

I often find myself dealing with data like this:
``` ruby
{ results: [{ id: 1, name: "foo" }, { id: 2, name: "bar" }, ... ] }
```
My problem is that I need to retrieve all the `id` values from the nested array of hashes, and I don't know how many there will be in advance.

It seems that the Find pattern could be expanded from allowing `pattern` (matching a single element) to `*pattern`. Examples:

``` ruby
# Base case
case { results: [{ id: 1, name: "foo" }, { id: 2, name: "bar" }] }
in results: [*{ id: ids }]
  "matched: #{ids}"
else
  "not matched"
end
#=> matched: [1, 2]

# With * at the end (rest of args) - same result
case { results: [{ id: 1, name: "foo" }, { id: 2, name: "bar" }] }
in results: [*{ id: ids }, *]
  "matched: #{ids}"
else
  "not matched"
end
#=> matched: [1, 2]

# When one element doesn't match and there is no *rest
case { results: [{ name: "foo" }, { id: 2, name: "bar" }] }
in results: [*{ id: ids }]
  "matched: #{ids}"
else
  "not matched"
end
#=> not matched
```

Similarly, `*Constant` could work to pull out types with an As pattern:

``` ruby
case [1, 2, 3, "string"]
in *Integer => nums, *
  "matched: #{nums}"
else
  "not matched"
end
#=> matched: [1, 2, 3]
```

Other patterns would work in the same way. Essentially, this expands the concept of `*` in pattern matching to mean "a variable number of `things matching subpattern`". Today, the only pattern supported by `*` is a variable binding - but it could be any of the other subpatterns as well.

This proposal does imply that this would work:
``` ruby
a = 2
[1, 2, 2, 3] in [*, *^a, *]
#=> true
```

To me, the `*` represents the variable number of matches, so the syntax makes intuitive sense. But others may have different opinions about `*^` being adjacent.

It may also imply this would work, though we could restrict the number of non-variable patterns (in other words, patterns that have the possibility of not matching) to 1 per Array so that this isn't possible.. I'm not sure something like this would be useful or clear.

``` ruby
a = 2
[1, 2, "hello", "ruby"] in [*Integer, *String]
#=> true
```

This feature feels like the missing piece of the Find pattern to me - I often want to "Find Multiple". If others agree, I would be happy to contribute by working on this feature and creating a pull request.



-- 
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/postorius/lists/ruby-core.ml.ruby-lang.org/

In This Thread