From: dblack@... Date: 2007-09-03T11:19:09+09:00 Subject: Re: assigning to hash keys when there is a default value? Hi -- On Mon, 3 Sep 2007, 7stud -- wrote: > Can someone explain why there is a difference in the second line of > output for the two hashes: > > h = Hash.new(5) > > puts h[2] > > h[2] ||= 10 > p h > > #---------- > puts > #---------- > > h = Hash.new > > puts h[2] > > h[2] ||= 10 > p h > > ---output:-- > 5 > {} > > nil > {2=>10} x ||= y is, I think, always supposed to be exactly equivalent to x = x || y, so that line in your first hash should be equivalent to: h[2] = 5 || 10 which should assign 5 to h[2]. It looks to me like you've found a bug. I can't think of any reason (and I really hope there isn't one, because having an exception to that ||= rule would be very messy) why using a default hash value would make any difference here. It's still 5 || 10 on the rhs, and it's still just an assignment. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)