1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Conditional Assignments

Leveraging the Shortcut Rule

By

Ruby provides a number of combined operation and assignment operators. For example, the += operator can be used as "a += 10" meaning "a = a + 10." They're in use everywhere, but there are two conditional assignment operators that aren't in wide use.

Or Equals: Assign If Left Hand is False

The "or equals" operator is used to assign a value to a variable if the variable's value evaluates to false. For example a ||= 10 is the equivalent to a = a || 10. To understand what's happening here, look at the conditional part of the statement. If a is true, the shortcut rule will be in place and the right hand side of the statement (in this case, just the literal '10') will never be evaluated. If a is false (either false or nil, as long as it evaluates to false) then the right-hand part of the statement will be evaluated and its value assigned to the variable a.

This is useful to make quick assignments to variables when you're not sure if they have a value already or not. If the variable already has a value then just keep moving on. It's the equivalent of a = 10 unless a.

And Equals: Assign If Left Hand is True

Now this one sounds a bit backwards. Take a &&= 10, which is roughly the same as a = 10 if a or a = a && 10. So if a already has a value, assign 10 to it. If it doesn't already have a value (is false or nil), then don't even evaluate the right hand side. What use is this?

Surely it's not as useful as ||=, which can compensate for missing values, filling them with sane defaults or procedurally generated values. It can however acts as a guard for methods that would otherwise fail. For example str &&= str.chomp. What is str is nil? The shortcut rule when applied to the && operator says that if the left hand side is false, you don't have to evaluate the right hand side. So if str is nil, it won't even try to call the chomp method on it (which will raise an exception).

Perl or Ruby

But the real question this makes you ask is "are you coding in Perl, or Ruby?" Perl used to be the king of scripting languages. It was extremely powerful and flexible, and you could do anything. This is before Python was popular, before Ruby was really known outside of Japan, when PHP was a barely adequate web language, and Javascript only existed in your browser. However, besides being a capable early (for this era, at least) scripting language, Perl is perhaps best known for "write only code." Perl used so many obscure symbols and was structured in such a way that it was very easy (and indeed seen as clever) to write extremely short and utterly unintelligible code. You can only write it, it's so dense you can't (or it is difficult) to read it.

But Ruby is not Perl. It's obviously inspired in part by Perl, and some Perl-isms have been carried over into Ruby. So should we use them? In part, these are simple and rather succinct Perlisms that can enhance your code's terseness and readability. But really, is a = 10 unless a really all that difficult? It's certainly more readable. And it might be rather easy to glass over the || and read it as a = 10 instead of a ||= 10, but it's rather difficult to miss an entire unless a.

Of all the Perlisms, these are among the least offensive. However, they're really just unnecessary. Use them if you wish, but they're just as easily replaced with simple conditional expressions.

  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Conditional Assignments

©2014 About.com. All rights reserved.