Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method · added in Ruby 4.1
ENV.fetch_values
ENV.fetch_values(*names) → array of values
ENV.fetch_values(*names) {|name| ... } → array of values
hash.c
View on GitHub ↗
static VALUE
env_fetch_values(int argc, VALUE *argv, VALUE ehash)
{
VALUE result = rb_ary_new2(argc);
long i;
for (i=0; i<argc; i++) {
rb_ary_push(result, env_fetch(1, &argv[i], ehash));
}
return result;
}
Returns an Array containing the environment variable values associated with the given names:
Example
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.fetch_values('foo', 'baz') # => ["0", "2"]
Otherwise if a block is given yields name to the block and returns the block’s return value:
Example
ENV.fetch_values('foo', 'bam') {|key| key.to_s} # => ["0", "bam"]
Raises KeyError if name is valid, but not found and block is not given:
Example
ENV.fetch_values('foo', 'bam') # Raises KeyError (key not found: "bam")
Returns an empty Array if no names given.
Raises an exception if any name is invalid. See Invalid Names and Values.
Ruby version changes
-
4.1
-
ENV.fetch_valuesis added. It returns an array of values for the given names, raisingKeyErrorfor missing names unless a block is given. [Feature #21781]
-