From: Matthew Harris Date: 2006-06-26T20:01:05+09:00 Subject: Re: << for Hash? I like Michael's solution. The {}'s are not needed, as long as the hash is the only or last argument to a method. {:a => 1, :b => 2}.merge(:c => 3, :d => 4) #=> {:a => 1, :b => 2, :c => 3, :d => 4) On 6/26/06, Michael Fellinger wrote: > {:foo,1}.merge! :bar => 2 > # {:foo=>1, :bar=>2} > > On Monday 26 June 2006 13:11, Simon Baird wrote: > > class Hash > > def <<(arg) > > self.merge!(arg) > > end > > end > > > > Eg, > > > > thing = { > > > > :foo => 1, > > > > } > > > > thing << { > > > > :bar => 2, > > : > > :baz => 3, > > > > } > > > > thing #=> {:foo=>1, :bar=>2, :baz=>3 } > > > > > > Of course it overwrites existing keys. I think it's a little sexier than > > > > thing.merge!({ > > > > :bar => 2, > > :baz => 3, > > > > }) > > > > or > > thing[:bar] = 1 > > thing[:baz] = 3 > > > > > > Is it safe to use this? Is there some good reason why << is not already > > defined for Hash? > > > > Simon > > (newbie) > > > > Ps, my first ruby project: > > http://tiddlyspot.com/ > > -- Matt