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

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

···

On 1/16/07, Josselin <josselin@wanadoo.fr> 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..)

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 16/01/07, Josselin <josselin@wanadoo.fr> 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

This was in the archives 4 days ago.

···

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

Just to be explicit, it's both 'nil' or 'false' that will be reset to
the new value:

irb(main):001:0> f = false
=> false
irb(main):003:0> f ||= 1
=> 1
irb(main):004:0> f
=> 1 # was set
irb(main):002:0> n = nil
=> nil
irb(main):005:0> n ||= 1
=> 1
irb(main):006:0> n
=> 1 # was set
irb(main):007:0> t = true
=> true
irb(main):008:0> t ||= 1
=> true
irb(main):009:0> t
=> true # wasn't set

HTH,
Keith

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

···

--

"a ||= b" means "a = b if a != nil"

"Josselin" <josselin@wanadoo.fr>
???:45accc4d$0$25937$ba4acef3@news.orange.fr...

···

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

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

joss

···

On 2007-01-16 14:12:42 +0100, Carlos <angus@quovadis.com.ar> said:

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

thansk Martin.. I can read Carlos' link with a good example !

Joss

···

On 2007-01-16 14:05:32 +0100, "Martin DeMello" <martindemello@gmail.com> said:

On 1/16/07, Josselin <josselin@wanadoo.fr> 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 <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

Thanks Farrel ... I'll use it now

···

On 2007-01-16 14:05:49 +0100, "Farrel Lifson" <farrel.lifson@gmail.com> said:

On 16/01/07, Josselin <josselin@wanadoo.fr> 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

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

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