From: "David A. Black" Date: 2009-08-27T08:23:44+09:00 Subject: Re: ||= with 1.8 and 1.9 ? Hi -- On Thu, 27 Aug 2009, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: ||= with 1.8 and 1.9 ?" > on Thu, 27 Aug 2009 06:23:01 +0900, "David A. Black" writes: > > |> According the rubyspec project: > |> > |> language/variables_spec.rb: > |> describe "Operator assignment 'var op= expr'" do > |> it "is equivalent to 'var = var op expr'" do > | > |It isn't, though, at least in the ||= case (see Joel W.'s post). > |Matz's characterization of it to me at RubyConf (or somewhere) was: > | > | x ||= y same as x || x = y > > Basically, "var op= expr" works like "var = var op expr" but left hand > side expression (var) is evaluated only once. For ||=, it is same as > > x = (x || y) > > but > > x || (x = y) > > is semantically same (well, almost). The hash case, though, definitely points to x || (x = y): >> h = Hash.new(5) => {} >> h[:x] = (h[:x] || 1) => 5 >> h => {:x=>5} >> h[:z] || (h[:z] = 1) => 5 >> h => {:x=>5} >> h[:y] ||= 1 => 5 >> h => {:x=>5} David -- David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com Q: What's the best way to get a really solid knowledge of Ruby? A: Come to our Ruby training in Edison, New Jersey, September 14-17! Instructors: David A. Black and Erik Kastner More info and registration: http://rubyurl.com/vmzN