Ruby idiom a ||= b means?

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

Thanks,
Ratnavel

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-edge-case

-Rob

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

Rob B. http://agileconsultingllc.com
[email protected]

On Jul 1, 6:49 am, Rob B. [email protected] 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

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.

This has been discussed nearly to death at:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fe4fbc48e19105cd/bf7f73380e285aff?lnk=gst&q=Or+equal#bf7f73380e285aff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs