Feature #10017
closed
Added by wojtekmach (Wojtek Mach) over 10 years ago.
Updated over 9 years ago.
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
fetch_at
sounds much better than values_at! to me.
- Description updated (diff)
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.
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
?
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.
For the reason Andrew pointed out, I also prefer #fetch_values
.
Maybe we need to conduct voting?
Matz.
-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
Should we get some more votes here, or should I just change my patch to use fetch_values?
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Matz, can we introduce this Hash#fetch_values
?
- 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.
- Subject changed from Add `Hash#fetch_values!` to Add `Hash#fetch_values`
#fetch_values approved.
Matz.
- 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
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0