Where to find info on ||= operator?


#1

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


#2

On 1/16/07, Josselin removed_email_address@domain.invalid wrote:

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…)

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


#3

On 16/01/07, Josselin removed_email_address@domain.invalid 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


#4

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.


#5

Josselin wrote:

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

Hi. Combine this:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UG

with this:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UE

HTH


#6

On 2007-01-16 14:12:42 +0100, Carlos removed_email_address@domain.invalid said:

joss

Hi. Combine this:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UG

with this:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UE

HTH

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

joss


#7

On 2007-01-16 14:05:49 +0100, “Farrel L.” removed_email_address@domain.invalid
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


#8

On 2007-01-16 14:05:32 +0100, “Martin DeMello” removed_email_address@domain.invalid
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


#9

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

“Josselin” removed_email_address@domain.invalid
???:45accc4d$0$25937$removed_email_address@domain.invalid…


#10

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