From: Robert Feldt Date: 2003-09-06T17:26:38+09:00 Subject: Re: Hash-ish and Arrays and Duplicates Meino Christian Cramer skrev den Sat, 6 Sep 2003 16:59:21 +0900: > Is there a method or ruby-"trick" to solve the problem ? > Thank you very much in advance for any help and/or hint ! :) > There are shorter solutions but here is one pretty clear one: class Hash def invert_with_duplicates nh = Hash.new {Array.new} self.each {|k,v| nh[v] = (nh[v].push k)} nh end end h = {:a => 1, :b => 1, :c => 2} p h.invert_with_duplicates # => {1=>[:a, :b], 2=>[:c]} Regards, /Robert Feldt