Project

General

Profile

Actions

Feature #21781

open

Add `fetch_values` method on `ENV`

Feature #21781: Add `fetch_values` method on `ENV`

Added by Earlopain (Earlopain _) about 22 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:124198]

Description

In https://bugs.ruby-lang.org/issues/10017, fetch_values was introduced for Hash. ENV is hash-like but is missing the method. Here are the differences between hash and env right now:

irb(main):006> Hash.instance_methods - ENV.methods
=> 
[:merge,
 :compare_by_identity,
 :compare_by_identity?,
 :>=,
 :deconstruct_keys,
 :dig,
 :<=,
 :>,
 :<,
 :to_proc,
 :flatten,
 :compact!,
 :default=,
 :default_proc,
 :default_proc=,
 :default,
 :transform_keys,
 :transform_keys!,
 :transform_values!,
 :fetch_values,
 :transform_values]

flatten/dig among others make no sense for ENV. Some I don't see why they are not present but I won't argue for them since I have no use-case.

Here is code I wanted to write:

# Get many env variables and crash if one is missing
user, pass, year, target_dir = ENV.fetch_values("USER", "PASS", "YEAR", "TARGET_DIR")

For now I am simply adding to_h and all is good. But should I have to? Ignoring a small amount of methods, ENV is very close to a hash already.


Related issues 1 (0 open1 closed)

Related to Ruby - Feature #10017: Add `Hash#fetch_values`Closedmatz (Yukihiro Matsumoto)Actions

Updated by Earlopain (Earlopain _) about 22 hours ago Actions #1

Actions

Also available in: PDF Atom