||= =|| — Or equals

That’s not just a cute little emoji describing how I feel on a typical day at Iron Yard; it’s without a doubt the most confusing logic operator in Ruby.

After researching this operator on stackoverflow I’ll attempt to explain.

Or equals, or equals, or equals…

a ||= b can be re-written in slightly longer form like this:

a || ( b = a )

Meaning…

If a is false, nil or undefined, then evaluate b and set it to a.

If a is defined or true, then the right side doesn’t run, and no assignment occurs. a remains a.

For further explanations see:

https://www.ruby-forum.com/topic/151660/

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.