From: Ryan Leavengood Date: 2005-11-23T07:33:21+09:00 Subject: Re: what is the ruby way to do this? On 11/22/05, ako... wrote: > hello, > > i am still learning. in the below code @attributes is a hash table. i > want to add a new value 'a' for a key represented by 'a.key'. i also > want to group values with the same keys in to arrays. thanks for any > help. > > konstantin > > if @attributes.has_key?(a.key) > if @attributes[a.key].is_a?(Array) > @attributes[a.key] << a > else > @attributes[a.key] = [@attributes[a.key], a] > end > else > @attributes[a.key] = a > end This is a fairly standard Ruby idiom (a hash of arrays): (@attributes[a.key]||=[])<