Where to find info on ||= operator?

I am using

http://www.ruby-doc.org/docs/ProgrammingRuby/

a sa reference manual, but could not find info on that operator (how
to use it…)

tfyl

joss

On 1/16/07, Josselin [email protected] wrote:

I am using

Programming Ruby: The Pragmatic Programmer's Guide

a sa reference manual, but could not find info on that operator (how
to use it…)

It’s syntax sugar:

foo = bar

expands to

foo = foo bar

So a ||= b expands to a = a || b, which (since || is short-circuiting)
means "set a to b if a is nil (or false), otherwise leave it at its
current value.

martin

On 16/01/07, Josselin [email protected] wrote:

a ||= b
is equivalent to
a = a || b
If a is nil it will be set to b. If it is not nil it will remain
unchanged.

Farrel

On 1/12/07, Keith F. <keith / audiobeta.com> wrote:

irb(main):002:0> n = nil
=> true # wasn’t set

HTH,
Keith

This was in the archives 4 days ago.

Josselin wrote:

I am using

Programming Ruby: The Pragmatic Programmer's Guide

a sa reference manual, but could not find info on that operator (how to
use it…)

tfyl

joss

Hi. Combine this:
Programming Ruby: The Pragmatic Programmer's Guide

with this:
Programming Ruby: The Pragmatic Programmer's Guide

HTH

On 2007-01-16 14:12:42 +0100, Carlos [email protected] said:

joss

Hi. Combine this:
Programming Ruby: The Pragmatic Programmer's Guide

with this:
Programming Ruby: The Pragmatic Programmer's Guide

HTH

thanks a lot… I should buy a hard-copy … easier to brwose than a
screen…

joss

On 2007-01-16 14:05:49 +0100, “Farrel L.” [email protected]
said:

joss

a ||= b
is equivalent to
a = a || b
If a is nil it will be set to b. If it is not nil it will remain unchanged.

Farrel

Thanks Farrel … I’ll use it now

On 2007-01-16 14:05:32 +0100, “Martin DeMello” [email protected]
said:

foo = bar

expands to

foo = foo bar

So a ||= b expands to a = a || b, which (since || is short-circuiting)
means "set a to b if a is nil (or false), otherwise leave it at its
current value.

martin

thansk Martin… I can read Carlos’ link with a good example !

Joss

“a ||= b” means “a = b if a != nil”

“Josselin” [email protected]
???:[email protected]

Wang Dong wrote:

“a ||= b” means “a = b if a != nil”

Mmm, no.

a ||= b is equivalent to:

a = b if a == nil or a == false

or

a = b if a.nil? or not a

or

a = b unless a

or simply

a = a || b

-Justin