[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby idiom a ||= b means?

Ratnavel P S

7/1/2008 4:32:00 AM

What does the ruby idiom a ||= b mean?

Thanks,
Ratnavel
--
Posted via http://www.ruby-....

4 Answers

Rob Biedenharn

7/1/2008 4:50:00 AM

0


On Jul 1, 2008, at 12:31 AM, Ratnavel P S wrote:

> What does the ruby idiom a ||= b mean?
>
> Thanks,
> Ratnavel

It is like:
a || a = b
See David A. Black's Blog
http://dablog.rubypal.com/2008/3/25/a-short-circuit...

-Rob

P.S. Please don't ask the same question on both Rails and Ruby lists/
forums.

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Lars Christensen

7/1/2008 8:27:00 AM

0

On Jul 1, 6:49 am, Rob Biedenharn <R...@AgileConsultingLLC.com> wrote:
> On Jul 1, 2008, at 12:31 AM, Ratnavel P S wrote:
>
> > What does the ruby idiom a ||= b mean?
>
> It is like:
>         a || a = b

In english, thats: If a is nil or false, then leave it, else assign a
= b.

Often used instead of code like this:

if a.nil? then
a = b
end

Dave Bass

7/1/2008 10:47:00 AM

0

Ratnavel P S wrote:
> What does the ruby idiom a ||= b mean?

It's commonly used to assign default values to things.
This idiom is also very common in Perl, which is presumably where it
came from.
--
Posted via http://www.ruby-....