Module: Gemmy::Patches::HashPatch::InstanceMethods::Inverse

Defined in:
lib/gemmy/patches/hash_patch.rb

Instance Method Summary collapse

Instance Method Details

#inverseObject

facets h = “b”=>3, “c”=>3, “d”=>2, “e”=>9, “f”=>3, “g”=>9 h.invert #=> 3=>“f”, 9=>“g” h.inverse #=> 3=>[“f”, “c”, “b”, “a”], 9=>[“g”, “e”] h.inverse.inverse #=> “b”=>3, “c”=>3, “d”=>2, “e”=>9, “f”=>3, “g”=>9



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/gemmy/patches/hash_patch.rb', line 200

def inverse
  i = Hash.new
  self.each_pair{ |k,v|
    if (Array === v)
      v.each{ |x| i[x] = ( i.has_key?(x) ? [k,i[x]].flatten : k ) }
    else
      i[v] = ( i.has_key?(v) ? [k,i[v]].flatten : k )
    end
  }
  return i
end