Hi -- On Fri, 2 May 2008, Phillip Gawlowski wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > David A. Black wrote: > > |> No, it means: x = x || y > | > | This is becoming a bit of a perma-thread :-) > | > | See Robert K.'s answer, and also have a look at the blog post I > | mentioned in my response. It's about exactly why x = x || y is not the > | right expansion for x ||= y. > > > x ||= y expands into x = x || y, since x == 1 expands to x = x + 1, and I think you mean +=. > we have no reason to expect ||= to behave differently (MPLOS). That's really where this thread always starts, though: why doesn't it behave like x = x || y in every case? Anyway, the thing about MPOLS is that you and I can't invoke it :-) It was actually Matz who showed me the x || x = y expansion (most recently at EuRuKo, where I talked to him about it because I wanted to make sure the corrections I made to my blog post had made it correct). > Also, it works as expected if the expansion x = x || y is correct. After > all: > > 'The or and || operators evaluate their first operand. If true, the > expression returns the value of their first operand; otherwise, the > expression returns the value of the second operand.' > Programming Ruby, 2nd Edition (p. 326). > > And Robert's answer works the way it works because the Hash has a > default value set, which doesn't equate to false or nil. If you override > the default value by setting the value to false, the assignment works > again as expected. Right, but hashes are allowed to have default values. So an explanation that accounts for that case is better than one that doesn't. The point is that if you do a drop-in replacement x = x || y you get a different result from ||=, and with x || x = y you don't. That means, definitively I think, that the latter is a more correct expansion. (I'm not sure how it couldn't mean that.) Anyway, this is at least the third thread on this in recent months, and I've written my blog post (and correction follow-up :-) about it. I think I'm about ||='d out. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!