From: Brian Candler Date: 2007-04-14T04:12:47+09:00 Subject: Re: Combining Hash elements to produce single line output On Sat, Apr 14, 2007 at 03:30:05AM +0900, Paul wrote: > tmp_hash = > [["foo:1", "123"], > ["foo:2", "abc"], > ["bar:1", "456"], > ["bar:2", "xyz"]] > > Desired Output: > --- > "foo" "123" "abc" > "bar" "456" "xyz" > --- # This is a hash which auto-creates non-existing members as an empty array new_hash = Hash.new { |h,k| h[k] = [] } tmp_hash.each do |key, value| key, rest = key.split(':', 2) new_hash[key] << value end new_hash.each do |key, values| puts '"' + ([key] + values).join('" "') + '"' end