Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

ENV.slice

ENV.slice(*names) → hash of name/value pairs

Returns a Hash of the given ENV names and their corresponding values:

Example
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2', 'bat' => '3')
ENV.slice('foo', 'baz') # => {"foo"=>"0", "baz"=>"2"}
ENV.slice('baz', 'foo') # => {"baz"=>"2", "foo"=>"0"}

Raises an exception if any of the names is invalid (see Invalid Names and Values):

Example
ENV.slice('foo', 'bar', :bat) # Raises TypeError (no implicit conversion of Symbol into String)
See also .size .store .shift .to_a also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0