On Sat, Apr 21, 2012 at 8:22 PM, David Heitzman <rubyinfo / aptifuge.com> wrote:
> Oh wow,  ¨Â óõâôìå äéóôéîãôéïî¬ âõéíðïòôáîô® äéîïëîï÷>
> if the left hand side is non-nil then no assignment happens at all.

More subtleties: if the left hand side is non falseish (i.e. not nil
and not false) the assignment will happen.

irb(main):008:0> h={:x=>false,:y=>nil}
=> {:x=>false, :y=>nil}
irb(main):009:0> h[:x] ||= 1
=> 1
irb(main):010:0> h[:y] ||= 2
=> 2
irb(main):011:0> h[:z] ||= 3
=> 3
irb(main):012:0> h
=> {:x=>1, :y=>2, :z=>3}

Note also that in case of a Hash the default value influences the result:

irb(main):016:0> h={:x=>false,:y=>nil}
=> {:x=>false, :y=>nil}
irb(main):017:0> h.default = 99
=> 99
irb(main):018:0> h[:x] ||= 1
=> 1
irb(main):019:0> h[:y] ||= 2
=> 2
irb(main):020:0> h[:z] ||= 3
=> 99
irb(main):021:0> h
=> {:x=>1, :y=>2}

And, to make things more complicated, there's also a default_proc:

irb(main):022:0> h = Hash.new {|ha,k| ha[k]=88}.merge(:x=>false,:y=>nil)
=> {:x=>false, :y=>nil}
irb(main):023:0> h[:x] ||= 1
=> 1
irb(main):024:0> h[:y] ||= 2
=> 2
irb(main):025:0> h[:z] ||= 3
=> 88
irb(main):026:0> h
=> {:x=>1, :y=>2, :z=>88}


Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/