Project

General

Profile

Actions

Feature #10017

closed

Add `Hash#fetch_values`

Added by wojtekmach (Wojtek Mach) over 10 years ago. Updated over 9 years ago.

Status:
Closed
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


Files

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

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #20702: Add `Array#fetch_values`ClosedActions

Updated by shyouhei (Shyouhei Urabe) over 10 years ago

fetch_at sounds much better than values_at! to me.

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Description updated (diff)

+1 for fetch_at.

Updated by wojtekmach (Wojtek Mach) over 10 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.

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 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.

Updated by matz (Yukihiro Matsumoto) over 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) over 10 years ago

one vote for #values_at

Updated by phluid61 (Matthew Kerwin) over 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) about 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.

Actions #15

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]
Actions #16

Updated by mame (Yusuke Endoh) 4 months ago

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0