Method: Immutable::Hash#invert

Defined in:
lib/immutable/_core.rb

#invertHash

Return a new Hash created by using keys as values and values as keys. If there are multiple values which are equivalent (as determined by #hash and #eql?), only one out of each group of equivalent values will be retained. Which one specifically is undefined.

Examples:

Immutable::Hash["A" => 1, "B" => 2, "C" => 3, "D" => 2].invert
# => Immutable::Hash[1 => "A", 3 => "C", 2 => "B"]

Returns:



659
660
661
662
663
# File 'lib/immutable/_core.rb', line 659

def invert
  pairs = []
  each { |k,v| pairs << [v, k] }
  self.class.new(pairs, &@default)
end