From: dblack@... Date: 2007-09-03T21:29:55+09:00 Subject: Re: assigning to hash keys when there is a default value? Hi -- On Mon, 3 Sep 2007, Russell Norris wrote: > I don't think this is a bug, kittens. since h[2] returns a value [even > though it's not set], it causes h[2] to evaluate so the assignment > never happens. x ||= y just means give me x or set x to y if there's > no value for x. h[2] _does_ have a value if only a default one. > > just my two cents. > > unless i missed the point here, in which case i apologize for my hasty > conclusion. :)) The thing is, this: x ||= y always means (at least, so I've always been told): x = x || y which in the case of the hash with a default value of 5, would mean: h[2] = 5 || 10 In other words: h.[]=(2, 5||10) At that point, the default value is out of the picture. The default value has no implications for the #[]= (writer) method; it's only what you get when you use the #[] (reader) method. So what's at stake here is the matter of x ||= y behaving predictably. I have to say, it's a case where I'm not even really concerned with the implementation (e.g., whether it uses [] on its way to []=) but just the semantics. I don't like the idea that the syntactic sugar is actually not a reliable drop-in replacement for the thing it's sugaring. 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)