From: Christoph Rippel Date: 2001-03-16T15:17:33+09:00 Subject: [ruby-talk:12715] Re: (long) Re: hash.invert loses data if equal values exist - is this the right behaviour? > From: Charles Hixson [mailto:charleshixsn@earthhlink.net] [...] > > Returns a new hash created by using hsh's values as keys, and the > > keys as values. Note that the result is uncertain if equvalent > > hsh's values are associated to different keys. > > > > h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 } > > h.invert #=> {200=>"d", 300=>"y", 0=>"a", 100=>"n"} > > > > Thanks, > > > > -- Gotoken > > What you want to return is a bag, not a set. So the general inverse of a > hash would have the second element be a list of values: > h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "e"=>200, "a" > => 0, "k"=>100} > h.invert #=> {200=>["d", "e"], 300=>["y"], 0=>["a"], 100=>["n", > "k"]} Just, out of curiosity is this the way CS-types use the term bag - or this all applied to the full-fledged thing - i.e. a general relation? Christoph