Skip to content
instance method

Hash#invert

() -> ::Hash[V, K]
invert → new_hash

Returns a new hash with each key-value pair inverted:

Example
h = {foo: 0, bar: 1, baz: 2}
h1 = h.invert
h1 # => {0=>:foo, 1=>:bar, 2=>:baz}

Overwrites any repeated new keys (see Entry Order):

Example
h = {foo: 0, bar: 0, baz: 0}
h.invert # => {0=>:baz}

Related: see Methods for Transforming Keys and Values.

See also #inspect #keep_if #initialize_copy #key also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0