Todd Benson
6/11/2008 9:24:00 PM
On Wed, Jun 11, 2008 at 4:12 PM, Lyle Johnson <lyle@lylejohnson.name> wrote:
> On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
>
>> what does ||= do?
>
> The expression:
>
> x ||= y
>
> is shorthand for:
>
> x = x || y
>
> where "||" is the OR-operator. If x evaluates as false or nil, the
> value of y will be assigned to x; otherwise, x keeps its original
> value. So, for example,
>
> x = nil
> x ||= 42 # now x has value 42
> x ||= 23 # x stays at 42
Not exactly.
a = 2
a ||= b #where b is not defined
p a #we get 2
p b #we get NameError
Not only that, but there are other subtleties that many gurus on this
list have pointed out.
Todd