From: James Britt Date: 2005-11-23T08:14:25+09:00 Subject: Re: what is the ruby way to do this? Bill Kelly wrote: > An alternate approach might be: > > @attributes = Hash.new {|h,k| h[k] = Array.new} > > This will automatically create arrays to hold your keys, on demand. > > Now you can just say: > > @attributes["whatever"] << "something" > @attributes["foo"] << 123 > @attributes["foo"] << 456 > > @attributes.inspect > {"foo"=>[123, 456], "whatever"=>["something"]} And, mixing some of the approaches shown: @attributes = {} # Assumes the given object quacks on 'key' def @attributes.<<( keyed_obj ) (self[keyed_obj.key]||=[])<