Project

General

Profile

Feature #10017

Add `Hash#fetch_values`

Added by wojtekmach (Wojtek Mach) over 3 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:63594]

Description

I'm proposing to add a new method Hash#values_at! that's basically a combination of #values_at and #fetch.

When dealing with configuration I prefer to use #fetch to fail early and this would be useful to read multiple keys at a time.
An example (not strictly on a Hash, but you get the idea) could be:

repo, token = ENV.values_at!('GITHUB_REPO', 'GITHUB_TOKEN')

ENV.values_at!('INVALID') # raises KeyError

# not sure if that useful, but I provide it here for the sake of completeness
ENV.values_at!("SHELL", "INVALID") { |k| k + " is missing" } # => ["/bin/bash", "INVALID is missing"]

Another name for this could be #fetch_at, perhaps.

P.S. I'm attaching a patch that was done using mostly trial and error

values_at_bang.patch (1.53 KB) values_at_bang.patch wojtekmach (Wojtek Mach), 07/08/2014 10:35 PM
fetch_at.patch (2.18 KB) fetch_at.patch wojtekmach (Wojtek Mach), 07/09/2014 06:52 PM

Associated revisions

Revision 50845
Added by nobu (Nobuyoshi Nakada) over 2 years ago

hash.c: fetch_values

  • hash.c (rb_hash_fetch_values): add Hash#fetch_values. [Feature #10017] [Fix GH-776]

Revision 50845
Added by nobu (Nobuyoshi Nakada) over 2 years ago

hash.c: fetch_values

  • hash.c (rb_hash_fetch_values): add Hash#fetch_values. [Feature #10017] [Fix GH-776]

Revision 50845
Added by nobu (Nobuyoshi Nakada) over 2 years ago

hash.c: fetch_values

  • hash.c (rb_hash_fetch_values): add Hash#fetch_values. [Feature #10017] [Fix GH-776]

History

#1 [ruby-core:63598] Updated by shyouhei (Shyouhei Urabe) over 3 years ago

fetch_at sounds much better than values_at! to me.

#2 [ruby-core:63605] Updated by nobu (Nobuyoshi Nakada) over 3 years ago

  • Description updated (diff)

+1 for fetch_at.

#3 [ruby-core:63612] Updated by wojtekmach (Wojtek Mach) over 3 years ago

I attached another patch calling this method fetch_at and I also added missing tests (I couldn't change issue title & body to reflect this changes, though).

Also, perhaps out of scope for this ticket but I'm wondering what're your thoughts about adding #fetch_at to ENV and Array too.

#4 [ruby-core:63613] Updated by avit (Andrew Vit) over 3 years ago

Please consider the name fetch_at carefully: it seems easily confused with fetch (1 key), and the name doesn't hint that it's for multiple keys. (values_at is plural, so it gives a good clue.) Maybe fetch_values?

#5 [ruby-core:64001] Updated by wojtekmach (Wojtek Mach) over 3 years ago

Andrew Vit wrote:

Please consider the name fetch_at carefully: it seems easily confused with fetch (1 key), and the name doesn't hint that it's for multiple keys. (values_at is plural, so it gives a good clue.) Maybe fetch_values?

Personally, I prefer fetch_at but fetch_values (or fetch_values_at) is also good!

I'm curious what other core developers think.

#6 [ruby-core:65032] Updated by matz (Yukihiro Matsumoto) about 3 years ago

For the reason Andrew pointed out, I also prefer #fetch_values.
Maybe we need to conduct voting?

Matz.

#7 [ruby-core:65143] Updated by jihwans (Jihwan Song) about 3 years ago

one vote for #values_at

#8 [ruby-core:65166] Updated by phluid61 (Matthew Kerwin) about 3 years ago

-1 for values_at! (this isn't a "!" method)

+0 for fetch_at (it's not clear that it isn't just an alias for #fetch)

+1 for fetch_values

#9 [ruby-core:65664] Updated by wojtekmach (Wojtek Mach) about 3 years ago

Should we get some more votes here, or should I just change my patch to use fetch_values?

#10 [ruby-core:66663] Updated by wojtekmach (Wojtek Mach) about 3 years ago

I submitted PR for this change: https://github.com/ruby/ruby/pull/776 calling the method fetch_values.

#11 [ruby-core:67943] Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Matz, can we introduce this Hash#fetch_values?

#12 [ruby-core:69451] Updated by wojtekmach (Wojtek Mach) over 2 years ago

  • Subject changed from Add `Hash#values_at!` to Add `Hash#fetch_values!`

Hi, please let me know if there's anything I can do to move this ticket forward.

#13 [ruby-core:69452] Updated by wojtekmach (Wojtek Mach) over 2 years ago

  • Subject changed from Add `Hash#fetch_values!` to Add `Hash#fetch_values`

#14 [ruby-core:69533] Updated by matz (Yukihiro Matsumoto) over 2 years ago

#fetch_values approved.

Matz.

#15 Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset r50845.


hash.c: fetch_values

  • hash.c (rb_hash_fetch_values): add Hash#fetch_values. [Feature #10017] [Fix GH-776]

Also available in: Atom PDF