[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

where to find info on ||= operator ?

Josselin

1/16/2007 1:00:00 PM

I am using

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

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

tfyl

joss

8 Answers

Martin DeMello

1/16/2007 1:06:00 PM

0

On 1/16/07, Josselin <josselin@wanadoo.fr> wrote:
> I am using
>
> http://www.ruby-doc.org/docs/Progra...
>
> 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

Farrel Lifson

1/16/2007 1:06:00 PM

0

On 16/01/07, Josselin <josselin@wanadoo.fr> wrote:
> I am using
>
> http://www.ruby-doc.org/docs/Progra...
>
> 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

Carlos

1/16/2007 1:13:00 PM

0

Josselin wrote:
> I am using
>
> http://www.ruby-doc.org/docs/Progra...
>
> 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/Progra...html/tut_expressions.html#UG

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

HTH
--


Josselin

1/16/2007 10:23:00 PM

0

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

> Josselin wrote:
>> I am using
>>
>> http://www.ruby-doc.org/docs/Progra...
>>
>> 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/Progra...html/tut_expressions.html#UG
>
> with this:
> http://www.ruby-doc.org/docs/Progra...html/tut_expressions.html#UE
>
> HTH

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

joss

Josselin

1/16/2007 10:23:00 PM

0

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
>>
>> http://www.ruby-doc.org/docs/Progra...
>>
>> 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

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

Joss

Josselin

1/16/2007 10:24:00 PM

0

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
>>
>> http://www.ruby-doc.org/docs/Progra...
>>
>> 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

Thanks Farrel ... I'll use it now

Wang Dong

1/17/2007 1:32:00 AM

0

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

"Josselin" <josselin@wanadoo.fr>
??????:45accc4d$0$25937$ba4acef3@news.orange.fr...
>I am using
>
> http://www.ruby-doc.org/docs/Progra...
>
> a sa reference manual, but could not find info on that operator (how to
> use it..)
>
> tfyl
>
> joss
>


Justin Collins

1/17/2007 3:23:00 AM

0

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