From: Robert Dober Date: 2008-04-06T04:55:52+09:00 Subject: Re: The ||= assignment operator On Sat, Apr 5, 2008 at 7:57 PM, Joel VanderWerf wrote: > David A. Black wrote: > > > > The only time this matters is with hashes that have default values. In > > every other case, as far as I know, x = x || y also describes what's > > happening. But the expansion which describes *every* case is x || x = > > y. > > > > One other case (a very similar one) in which it matters is this: > > class C > def x; @x || 5; end > def x=(v); puts "assigning @x=#{v}"; @x = v; end > end > > c = C.new > c.x ||= 3 # no effect > p c.x # 5 > c.x = 2 # assigning @x=2 > p c.x # 2 > > -- > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 > > Hmm I am not sure I believe you rushed your example a little bit c.x = c.x || a and c.x || c.x = a seem to me having the same effect. I only came up with a perverse example to show the difference class C count = 0 def x; true end define_method :x= do |_| count += 1 @x = count end end c = C.new c.x || c.x = 42 p c c.x = c.x || 42 p c c.x ||= 42 p c Cheers Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein