How to Use The Ruby Ternary Operator (?:)

What is a ternary operator in Ruby?

A ternary operator is made of three parts, that’s where the word “ternary” comes from. These parts include a conditional statement & two possible outcomes.

In other words, a ternary gives you a way to write a compact if/else expression in just one line of code.

For example:

if apple_stock > 1
  :eat_apple
else
  :buy_apple
end

Can become this:

apple_stock > 1 ? :eat_apple : :buy_apple

Thanks to the ternary operator!

Question…

How does this work, exactly & what is the proper syntax?

Let’s find out.

A Template For Writing Your Own Ternary

If we generalize the syntax for a ternary operator you get a “fill in the blanks” kind of template.

It looks like this:

condition ? true : false

The first part of a ternary operator is the condition, as in the condition you want to check if it’s true or not.

After that, we have a question mark (?).

That’s part of the syntax!

It’s how Ruby knows that you’re writing a ternary operator.

Next:

We have whatever code you want to run if the condition turns out to be true, the first possible outcome.

Then a colon (:), another syntax element.

Lastly, we have the code you want to run if the condition is false, the second possible outcome.

Example:

"chocolate".size > 4 ? "More than four characters" : "Less than four characters"
  • Condition => "chocolate".size > 4
  • If True => "More than four characters"
  • Else => "Less than four characters"

These three parts are the components of every ternary expression.

Don’t forget your ? between the condition & the possible outcomes, even if the method name ends in a question mark.

Example:

"".empty? ? "Yes" : "No"

This one is easy to overlook, so pay attention when writing this kind of code.

Complex Ternary Operators

The ternary operator can be used incorrectly.

It starts to become a bad idea whenever you want to do something complex with it.

For example:

You can assign the result of a ternary expression to a variable.

a = 10 > 5 ? "yes" : "no"

That’s fine.

But it starts getting more complicated when your possible outcomes have spaces in them.

Here’s an example:

10 > 5 ? (puts "yes") : (puts "no")

We have to use the parenthesis here.

Why?

Because if you don’t, you get a SyntaxError.

These parentheses make this code a little harder to understand, which takes away from the readability & style benefits of using a ternary.

Keep that in mind when choosing to use a ternary or not.

Ternary Operators & Operator Precedence

Another issue that comes up with ternary expressions is operator precedence.

Here’s an example:

[].tap { |a| a << "ccccc".size == 0 ? "empty" : "not empty" }

# [5]

I would expect either "empty" or "not empty", the two possible outcomes for our ternary, but we got 5 instead.

What's going on?

It turns out that Ruby & most programming languages process expressions with different levels of priority.

This happens even in simple arithmetic!

As you know, multiplication is evaluated before addition & subtraction.

Similarly, in Ruby, a method call (<< & size) takes precedence over a ternary operator, which explains the behavior in the last example.

You can solve this with parenthesis:

[].tap { |a| a << ("bbbbb".size == 0 ? "empty"  : "not empty") }

# ["not empty"]

Another thing to keep in mind when writing your ternary operators!

Video Tutorial

Summary

You've learned about the ternary conditional operator in Ruby, this operator allows you to write compact conditional statements which can make your code easier or harder to read depending on the situation.

Now it's your turn! Practice what you learned so you can install this knowledge into your brain.

Thanks for reading 🙂