Forum: Ruby where to find info on ||= operator ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-01-16 14:01
(Received via mailing list)
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
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-01-16 14:06
(Received via mailing list)
On 1/16/07, Josselin <josselin@wanadoo.fr> 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 <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
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2007-01-16 14:07
(Received via mailing list)
On 16/01/07, Josselin <josselin@wanadoo.fr> 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
35b0b4029fd4387842ec88a8e99d84de?d=identicon&s=25 Jason Mayer (Guest)
on 2007-01-16 14:12
(Received via mailing list)
On 1/12/07, Keith Fahlgren <keith / audiobeta.com> wrote:
> irb(main):002:0> n = nil
> => true  # wasn't set
>
> HTH,
> Keith

This was in the archives 4 days ago.
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 Carlos (Guest)
on 2007-01-16 14:13
(Received via mailing list)
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/...

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

HTH
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-01-16 23:26
(Received via mailing list)
On 2007-01-16 14:05:32 +0100, "Martin DeMello" <martindemello@gmail.com>
said:

> 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
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-01-16 23:26
(Received via mailing list)
On 2007-01-16 14:12:42 +0100, Carlos <angus@quovadis.com.ar> said:

>> joss
>
> Hi. Combine this:
>    http://www.ruby-doc.org/docs/ProgrammingRuby/html/...
>
> with this:
>    http://www.ruby-doc.org/docs/ProgrammingRuby/html/...
>
> HTH

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

joss
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-01-16 23:27
(Received via mailing list)
On 2007-01-16 14:05:49 +0100, "Farrel Lifson" <farrel.lifson@gmail.com>
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
6be68ea08f17703ec184c0a861524c4a?d=identicon&s=25 Wang Dong (wangdong)
on 2007-01-17 02:35
(Received via mailing list)
"a ||= b" means "a = b if a != nil"

"Josselin" <josselin@wanadoo.fr>
??????:45accc4d$0$25937$ba4acef3@news.orange.fr...
F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (justincollins)
on 2007-01-17 04:23
(Received via mailing list)
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
This topic is locked and can not be replied to.