From: Eleanor McHugh Date: 2009-05-06T22:43:51+09:00 Subject: Re: '=||' On 6 May 2009, at 13:03, Rick DeNatale wrote: > The short circuiting only happens if the lhs expression returns a > non-truthy value. > > Now it's true (I think) that "a op= b" is the same as "a = a op b" in > C, Ruby ain't C. :) > Sometimes the stuff "under the hood" is a little more complicated than > it first appears. Very true, and we all learn new stuff by considering it. For example I suspect quite a few people would be surprised by the following code: class Test def initialize @switch = false end def [] x puts "[#{x}]" @switch = !@switch end def []= x, y puts "[#{x}] = #{y}" end end t = Test.new t[:a] ||= 17 t[:a] ||= 17 t[:a] ||= 17 output: [a] [a] [a] here the '[]' call returns 'true' and 'false' by turns but in either event the assignment fails to call '[]='... Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason