* [ruby-core:118969] [Ruby master Feature#20702] Add `Array#fetch_values`
@ 2024-08-27 20:13 toy (Ivan Kuchin) via ruby-core
2024-09-05 8:46 ` [ruby-core:119060] " matz (Yukihiro Matsumoto) via ruby-core
2024-09-05 18:35 ` [ruby-core:119070] " byroot (Jean Boussier) via ruby-core
0 siblings, 2 replies; 3+ messages in thread
From: toy (Ivan Kuchin) via ruby-core @ 2024-08-27 20:13 UTC (permalink / raw)
To: ruby-core; +Cc: toy (Ivan Kuchin)
Issue #20702 has been reported by toy (Ivan Kuchin).
----------------------------------------
Feature #20702: Add `Array#fetch_values`
https://bugs.ruby-lang.org/issues/20702
* Author: toy (Ivan Kuchin)
* Status: Open
----------------------------------------
`Array` and `Hash` have matching methods to fetch:
* One value using `[]` (`Array` also allows fetching multiple with range or two arguments)
* One value with default or exception using `fetch` method
* Multiple values using `values_at` method
But only `Hash` has method `fetch_values` to fetch multiple values with fallback or raising an exception (see #10017).
```ruby
hash = {a: 1, b: 2, c: 3}
array = [1, 2, 3]
hash[:b] # => 2
hash[:d] # => nil
array[1] # => 2
array[4] # => nil
hash.fetch(:b) # => 2
hash.fetch(:d) # => IndexError
hash.fetch(:d){ 42 } # => 42
array.fetch(1) # => 2
array.fetch(4) # => IndexError
array.fetch(4){ 42 } # => 42
hash.values_at(:b, :c) # => [2, 3]
hash.values_at(:b, :d) # => [2, nil]
array.values_at(1, 2) # => [2, 3]
array.values_at(1, 4) # => [2, nil]
hash.fetch_values(:b, :c) # => [2, 3]
hash.fetch_values(:b, :d) # => IndexError
hash.fetch_values(:b, :d){ 42 } # => [2, 42]
# missing
array.fetch_values(1, 2) # => [2, 3]
array.fetch_values(1, 4) # => IndexError
array.fetch_values(1, 4){ 42 } # => [2, 42]
```
--
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/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:119060] [Ruby master Feature#20702] Add `Array#fetch_values`
2024-08-27 20:13 [ruby-core:118969] [Ruby master Feature#20702] Add `Array#fetch_values` toy (Ivan Kuchin) via ruby-core
@ 2024-09-05 8:46 ` matz (Yukihiro Matsumoto) via ruby-core
2024-09-05 18:35 ` [ruby-core:119070] " byroot (Jean Boussier) via ruby-core
1 sibling, 0 replies; 3+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2024-09-05 8:46 UTC (permalink / raw)
To: ruby-core; +Cc: matz (Yukihiro Matsumoto)
Issue #20702 has been updated by matz (Yukihiro Matsumoto).
Looks like a good idea. Accepted.
Matz.
----------------------------------------
Feature #20702: Add `Array#fetch_values`
https://bugs.ruby-lang.org/issues/20702#change-109638
* Author: toy (Ivan Kuchin)
* Status: Open
----------------------------------------
`Array` and `Hash` have matching methods to fetch:
* One value using `[]` (`Array` also allows fetching multiple with range or two arguments)
* One value with default or exception using `fetch` method
* Multiple values using `values_at` method
But only `Hash` has method `fetch_values` to fetch multiple values with fallback or raising an exception (see #10017).
```ruby
hash = {a: 1, b: 2, c: 3}
array = [1, 2, 3]
hash[:b] # => 2
hash[:d] # => nil
array[1] # => 2
array[4] # => nil
hash.fetch(:b) # => 2
hash.fetch(:d) # => IndexError
hash.fetch(:d){ 42 } # => 42
array.fetch(1) # => 2
array.fetch(4) # => IndexError
array.fetch(4){ 42 } # => 42
hash.values_at(:b, :c) # => [2, 3]
hash.values_at(:b, :d) # => [2, nil]
array.values_at(1, 2) # => [2, 3]
array.values_at(1, 4) # => [2, nil]
hash.fetch_values(:b, :c) # => [2, 3]
hash.fetch_values(:b, :d) # => IndexError
hash.fetch_values(:b, :d){ 42 } # => [2, 42]
# missing
array.fetch_values(1, 2) # => [2, 3]
array.fetch_values(1, 4) # => IndexError
array.fetch_values(1, 4){ 42 } # => [2, 42]
```
--
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/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:119070] [Ruby master Feature#20702] Add `Array#fetch_values`
2024-08-27 20:13 [ruby-core:118969] [Ruby master Feature#20702] Add `Array#fetch_values` toy (Ivan Kuchin) via ruby-core
2024-09-05 8:46 ` [ruby-core:119060] " matz (Yukihiro Matsumoto) via ruby-core
@ 2024-09-05 18:35 ` byroot (Jean Boussier) via ruby-core
1 sibling, 0 replies; 3+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-09-05 18:35 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20702 has been updated by byroot (Jean Boussier).
Implementation: https://github.com/ruby/ruby/pull/11555
----------------------------------------
Feature #20702: Add `Array#fetch_values`
https://bugs.ruby-lang.org/issues/20702#change-109653
* Author: toy (Ivan Kuchin)
* Status: Open
----------------------------------------
`Array` and `Hash` have matching methods to fetch:
* One value using `[]` (`Array` also allows fetching multiple with range or two arguments)
* One value with default or exception using `fetch` method
* Multiple values using `values_at` method
But only `Hash` has method `fetch_values` to fetch multiple values with fallback or raising an exception (see #10017).
```ruby
hash = {a: 1, b: 2, c: 3}
array = [1, 2, 3]
hash[:b] # => 2
hash[:d] # => nil
array[1] # => 2
array[4] # => nil
hash.fetch(:b) # => 2
hash.fetch(:d) # => IndexError
hash.fetch(:d){ 42 } # => 42
array.fetch(1) # => 2
array.fetch(4) # => IndexError
array.fetch(4){ 42 } # => 42
hash.values_at(:b, :c) # => [2, 3]
hash.values_at(:b, :d) # => [2, nil]
array.values_at(1, 2) # => [2, 3]
array.values_at(1, 4) # => [2, nil]
hash.fetch_values(:b, :c) # => [2, 3]
hash.fetch_values(:b, :d) # => IndexError
hash.fetch_values(:b, :d){ 42 } # => [2, 42]
# missing
array.fetch_values(1, 2) # => [2, 3]
array.fetch_values(1, 4) # => IndexError
array.fetch_values(1, 4){ 42 } # => [2, 42]
```
--
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/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-09-05 18:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-27 20:13 [ruby-core:118969] [Ruby master Feature#20702] Add `Array#fetch_values` toy (Ivan Kuchin) via ruby-core
2024-09-05 8:46 ` [ruby-core:119060] " matz (Yukihiro Matsumoto) via ruby-core
2024-09-05 18:35 ` [ruby-core:119070] " byroot (Jean Boussier) via ruby-core
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).