Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!talisker.lacave.net!lacave.net!not-for-mail From: Rick DeNatale Newsgroups: comp.lang.ruby Subject: Re: Please explain nuances of ||= Date: Thu, 1 May 2008 20:26:09 -0500 Organization: Service de news de lacave.net Lines: 66 Message-ID: References: <60c659cd-f1a6-4a1b-a614-90aaee7ae467@i76g2000hsf.googlegroups.com> <87wsmdx3bh.fsf@xts.gnuu.de> <481A2A1F.9080301@gmail.com> <481A31DD.8070701@gmail.com> <481A3D4D.5040709@gmail.com> <481A4B5F.1050307@path.berkeley.edu> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1209691581 70590 65.111.164.187 (2 May 2008 01:26:21 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 2 May 2008 01:26:21 +0000 (UTC) In-Reply-To: <481A4B5F.1050307@path.berkeley.edu> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 300344 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: 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/