Skip to content

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

class method

ENV.invert

ENV.invert → hash of value/name pairs

Returns a Hash whose keys are the ENV values, and whose values are the corresponding ENV names:

Example
ENV.replace('foo' => '0', 'bar' => '1')
ENV.invert # => {"1"=>"bar", "0"=>"foo"}

For a duplicate ENV value, overwrites the hash entry:

Example
ENV.replace('foo' => '0', 'bar' => '0')
ENV.invert # => {"0"=>"foo"}

Note that the order of the ENV processing is OS-dependent, which means that the order of overwriting is also OS-dependent. See About Ordering.

See also .inspect .keep_if .include? .key also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1