From: Robert Klemme Date: 2008-05-02T16:39:31+09:00 Subject: Re: Please explain nuances of ||= 2008/5/2 Rick DeNatale : > 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. Nice summary! And, if you think about it from a usability perspective it does make the most sense the way it is. 1. you do not want to spent CPU cycles for assigning the same object to a reference 2. more importantly, in the case of Hash you do not want the Hash to change (which it would do if ||= were implemented differently as I have demonstrated earlier). I'm out. Cheers robert -- use.inject do |as, often| as.you_can - without end