where to find info on ||= operator ?

M

Martin DeMello

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 <operator>= bar

expands to

foo = foo <operator> 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
 
J

Josselin

It's syntax sugar:

foo <operator>= bar

expands to

foo = foo <operator> 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
 
J

Justin Collins

Wang said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top