From: "Jesús Gabriel y Galán" Date: 2010-01-19T00:31:50+09:00 Subject: Re: What does ||= represent in this situation? On Mon, Jan 18, 2010 at 2:27 PM, John Merlino wrote: > anler hp wrote: >> hello, the operator | | = is the same as the typical + =, -= and so >> on, why did exist in ruby and not in other languages is because in >> ruby any expression returns a value. >> here is an example very closed but pretty much less effective in other >> language >> >> a = false; >> a || a = c; >> >> this means, makes a = c only if a is false�, same things happen with >> >> a ||= c  -- equivalent to --> a = a || c (if a is false, the right >> side of the || will be evaluated and its value will be returned -this >> only happen in ruby-, so c is returned either true or false) >> >> sorry for my english > > So if a is true, it is returned. If a is false, c is returned. What if a > is undefined? irb(main):002:0> defined? a => nil irb(main):003:0> a ||= "hello" => "hello" irb(main):004:0> a => "hello" irb(main):005:0> defined? a => "local-variable" Think of it as an idiom to set a variable to a value only the first time. The only caveat is to take care when false is a valid value for such variable. Jesus.