On Mon, Jan 08, 2007 at 08:20:05PM +0900, Robert Klemme wrote: > On 08.01.2007 02:59, dblack / wobblini.net wrote: > >On Mon, 8 Jan 2007, Florian Gross wrote: > >>On 2007-01-08 02:00:10 +0100, dblack / wobblini.net said: > >> > >>>hsh[:key] = "value" unless hsh.has_key? :key > >>>You can use merge and a one-key hash: > >>> > >>> hash.merge({ :key => "new value" }) > >> > >>Hm, not really: > >> > >>{ :key => "old value" }.merge({ :key => "new value" }) # => > >>{:key=>"new value"} > > > >True -- that's a bit of a deal-breaker :-) > > But only if you need the return value: > > irb(main):001:0> hash={:key=>1} > => {:key=>1} > irb(main):002:0> hash.merge( :key => "new value" ) > => {:key=>"new value"} > irb(main):003:0> hash > => {:key=>1} > > The hash is merged properly. The original hash is not modified by #merge. #merge != #merge! (== #update) a = {} a.merge(:foo => 1) # => {:foo=>1} a # => {} -- Mauricio Fernandez - http://eigenclass.org - singular Ruby