From: dblack@... Date: 2007-01-08T21:37:05+09:00 Subject: Re: a most undangerous Hash#store! Hi -- On Mon, 8 Jan 2007, Mauricio Fernandez wrote: > 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 # => {} Yes, merge was just a wrong turn on my part. Forget it. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)