Feature #10017
closedAdd `Hash#fetch_values`
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
Files
Updated by shyouhei (Shyouhei Urabe) over 10 years ago
fetch_at
sounds much better than values_at! to me.
Updated by wojtekmach (Wojtek Mach) over 10 years ago
- File fetch_at.patch fetch_at.patch added
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.
Updated by avit (Andrew Vit) over 10 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
?
Updated by wojtekmach (Wojtek Mach) over 10 years ago
Andrew Vit wrote:
Please consider the name
fetch_at
carefully: it seems easily confused withfetch
(1 key), and the name doesn't hint that it's for multiple keys. (values_at
is plural, so it gives a good clue.) Maybefetch_values
?
Personally, I prefer fetch_at
but fetch_values
(or fetch_values_at
) is also good!
I'm curious what other core developers think.
Updated by matz (Yukihiro Matsumoto) about 10 years ago
For the reason Andrew pointed out, I also prefer #fetch_values
.
Maybe we need to conduct voting?
Matz.
Updated by jihwans (Jihwan Song) about 10 years ago
one vote for #values_at
Updated by phluid61 (Matthew Kerwin) about 10 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
Updated by wojtekmach (Wojtek Mach) about 10 years ago
Should we get some more votes here, or should I just change my patch to use fetch_values?
Updated by wojtekmach (Wojtek Mach) almost 10 years ago
I submitted PR for this change: https://github.com/ruby/ruby/pull/776 calling the method fetch_values
.
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Matz, can we introduce this Hash#fetch_values
?
Updated by wojtekmach (Wojtek Mach) over 9 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.
Updated by wojtekmach (Wojtek Mach) over 9 years ago
- Subject changed from Add `Hash#fetch_values!` to Add `Hash#fetch_values`
Updated by matz (Yukihiro Matsumoto) over 9 years ago
#fetch_values approved.
Matz.
Updated by nobu (Nobuyoshi Nakada) over 9 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]
Updated by mame (Yusuke Endoh) 2 months ago
- Related to Feature #20702: Add `Array#fetch_values` added