From: Robert Klemme Date: 2006-06-26T18:40:14+09:00 Subject: Re: << for Hash? 2006/6/26, Simon Baird : > 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? The reason is probably that your << appends a complete collection (a hash) to the hash while usually it appends just a single element (see for example Array#<< and IO#<<). While I think about it, String#<< appends the same type (String) to a String... Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/