From: Ryan Leavengood Date: 2005-06-27T04:34:43+09:00 Subject: Re: grouping/sorting [newbie question] bdarcus@gmail.com wrote: > > E.g. it only creates a new "group" if one doesn't already exist? Was > wondering how to do that, so thanks for this! That is one way. Hashes can also take a block in their constructors which can be used for default values: h = Hash.new {|h,k| h[k] = 'default'} h['foo'] = 'bar' p h['foo'] # => "bar" p h['baz'] # => "default" For the code I wrote before, it would be like this: grouped_comments = films.inject(Hash.new {|h,k| h[k] = []}) do |grouped, film| grouped[film['title']] << film['comments'] grouped end But in this case I prefer the ||= syntax. Ryan