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.
Josselin (Guest)
on 2007-01-16 15: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
Martin DeMello (Guest)
on 2007-01-16 15:06
(Received via mailing list)
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 <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 L. (Guest)
on 2007-01-16 15:07
(Received via mailing list)
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
Jason M. (Guest)
on 2007-01-16 15:12
(Received via mailing list)
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.
Carlos (Guest)
on 2007-01-16 15: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
Josselin (Guest)
on 2007-01-17 00:26
(Received via mailing list)
On 2007-01-16 14:05:32 +0100, "Martin DeMello" 
<removed_email_address@domain.invalid>
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
Josselin (Guest)
on 2007-01-17 00:26
(Received via mailing list)
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/...
>
> 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
Josselin (Guest)
on 2007-01-17 00:27
(Received via mailing list)
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
Wang D. (Guest)
on 2007-01-17 03:35
(Received via mailing list)
"a ||= b" means "a = b if a != nil"

"Josselin" <removed_email_address@domain.invalid>
??????:45accc4d$0$25937$removed_email_address@domain.invalid...
Justin C. (Guest)
on 2007-01-17 05: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.