From: Rick DeNatale Date: 2008-05-02T10:26:09+09:00 Subject: Re: Please explain nuances of ||= Someone already mentioned it, but I recently wrote an article about this controversy: http://talklikeaduck.denhaven2.com/articles/2008/04/26/x-y-redux In writing this article, I pulled my old dusty copy of Kernighan and Ritchie off my shelf, since IIRc the whole x op= y idea originally came from C. And of course that's where the oft reported meme that x op=y was identical to x = x op y originated, and was propagated through several popular expositions of Ruby. BUT C, although it does have || and && as short-circuit boolean operators, does NOT allow ||= or &&= When Matz added these to Ruby he, quite cleverly IMHO, defined them as short circuiting as well, the assignment is NOT done if the LHS evaluates to true (in the Ruby sense that is, neither nil nor false). And as I also verified, since reading about ||= in David Flanigan's "The Ruby Programming Language" got me to thinking about it, &&= also doesn't do an assignment if the LHS evaluates to false. All of this falls out naturally from 1) short-circuit evaluation of || and &&, and 2) the fact that a.x = y is syntactic sugar for a.x=(y), and 3) a[b] = c is syntactic sugar for a.[]=(b,c). So a foolish adherence to the idea that Ruby should slavishly follow C where ||= and &&= are not allowable assignment operators, and there are no x= or []= methods to serve as targets of syntactic sugar, doesnt make that sense. So: 1) In Ruby x ||= y does no assignment if x evaluates to true, and x &&= y does no assignment if x evaluates to true. 2) x || x = y, and x && x = y are much better first approximations of how x ||= y and x &&= y than x = x || y, and x = x && y, despite what K&R say. 3) I say first approximations because of David Black's observation that x ||= y and x || x = y differ when x is undefined. In summary: That's the way that Ruby works. Ruby ain't C, or C++ or Java. Get over it! -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/