invert()
public
Returns a new hash created by using hsh’s values as keys, and the keys as values. If a key with the same value already exists in the hsh, then the last one defined will be used, the earlier value(s) will be discarded.
h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 } h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
If there is no key with the same value, Hash#invert is involutive.
h = { a: 1, b: 3, c: 4 } h.invert.invert == h #=> true
The condition, no key with the same value, can be tested by comparing the size of inverted hash.
# no key with the same value h = { a: 1, b: 3, c: 4 } h.size == h.invert.size #=> true # two (or more) keys has the same value h = { a: 1, b: 3, c: 1 } h.size == h.invert.size #=> false