From: Russell Norris Date: 2007-09-03T22:32:21+09:00 Subject: Re: assigning to hash keys when there is a default value? I learned that x ||= y means set x to y unless x, so I don't see the bug. and i don't see this as being at all unpredictable. either way you expect it to work, it either fails 100% or succeeds 100%. :) the only problem is when you expect it to do something it doesn't do, which is tautologically what every problem is i guess. RSL On 9/3/07, dblack@wobblini.net wrote: > 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) > >