From: Martin DeMello Date: 2005-12-01T22:12:31+09:00 Subject: Re: Help constructing interesting hash? mental@rydia.net wrote: > > def build2( values, keys ) > hash = {} > values.zip( keys ) do |value, key| > ( hash[key] ||= [] ).push value > end > hash > end > > build2(*[[A, B, C], [1, 2, 2]]) #=> {1=>[A], 2=>[B, C]} The other interesting way is def build3(values, keys) hash = Hash.new {|h,k| h[k] = []} values.zip(keys) do |value, key| hash[key].push value end hash end i.e. pushing the responsibility of generating a [] the first time a key is seen off onto the hash rather than the insertion code. martin