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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0