Skip to content

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

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

  1. 4.1