From: Robert Klemme Date: 2009-12-23T17:28:00+09:00 Subject: Re: newbie: if is not null else... 2009/12/23 Stefano Crocco : > On Wednesday 23 December 2009, Joshua Ballanco wrote: >> |> $x ||= '-' >> |> >> |> >> |> >> |> This is a short form for >> |> >> |> >> |> >> |> $x = $x || '-' >> | >> |Correction, it is short form for: >> | >> |$x || $x = '-' >> | >> |...trust me, we've been over this at least 3 times, at great length, on >> |the mailing list. ;-) > > I may be missing something, but according to page 96 of "The Ruby Programming > Language", my version seems to be the correct one. It states that: > > results ||= [] > > expands to > > results = results || [] For a simple assignment the difference is negligible but it matters if you do h = {} h[1] ||= 2 Please search the archives, Josh is right. Btw, you can easily prove it by doing irb(main):001:0> x = Class.new { def a;puts "a";@a;end; def a=(x);puts "a=";@a=x;end }.new => #<#:0x10134d10> irb(main):002:0> x.a ||= 123 a a= => 123 irb(main):003:0> x.a ||= 456 a => 123 irb(main):004:0> x.a = x.a || 789 a a= => 123 irb(main):005:0> If you were right the output of line 3 would look like the one of line 4. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/