From: Christoph Rippel Date: 2001-03-07T01:18:00+09:00 Subject: [ruby-talk:12153] Re: (long) Re: hash.invert loses data if equal values exist - is this the right behaviour? > From: Tammo Freese [mailto:tammo.freese@offis.de] [...] > Or is the morale that I should not invert hashes with equal values > because the results are too difficult to predict? FYI - hash.invert is a ``left inverse'' under composition regardless of the ambiquites of it's actually value ... class Hash def * (other) res = clone each do |l,r| raise 'composition undefined' unless other.key? r res[l] = other[r] end res end end hsh = Hash[1, 3, 2, 3,4,4,"a",4 ] ish = hsh.clone.invert p ish*hsh # {4=>4, 3=>3} Christoph