Ternary Operator
Explore the use of the ternary operator in Ruby to write compact conditional statements. Understand its syntax, practical examples, and when to prefer it over traditional if...else structures. This lesson helps you write clearer and shorter code by using ternary expressions effectively.
We'll cover the following...
Use of ternary operators
The ternary operator is a useful feature in any language. It’s implemented in Ruby the same way that it is implemented in C, Java, Python, JavaScript, and so on. Some programmers have been using this operator for a long time, but aren’t familiar with its name. But we recommend remembering its name, because it’s always nice to use the correct names and terminology.
Despite the intimidating name, its syntax is quite straightforward:
something_is_truthy ? do_this() : else_this()
For example:
is_it_raining? ? stay_home() : go_party()
This is 100% the same as the following code, but with the inclusion of if...else:
Note that we can always omit empty parentheses in Ruby in favor of code readability. Parentheses indicate that we’re calling a method with no arguments or parameters. But Ruby is smart enough to understand what we mean:
Or just:
Notice the double question marks in the single line of code shown above. This is intentional. We should just assume that there is a method with a question mark defined somewhere:
def is_it_raining?
# ... do something, like a call to external weather service ...
end
The is_it_raining? method always returns true or false. In other words, it returns a boolean type. Ruby’s naming conventions state that if a method returns a boolean type, then we should define this method with a question mark at the end, so that it looks like a question written in plain English. For example: “Is it raining?”
However, if the result depends on some variable, the one-liner ternary operator may look like the below example. Note the single question mark only, which is mandatory for ternary operator):
x ? stay_home() : go_party()
Or just:
x ? stay_home : go_party
As we can see from the examples given above, the ternary operator has a more compact syntax and can save us a couple of lines on the screen. As a result, our program will look less lengthy and,hopefully, appear more readable to others. The only disadvantage of a ternary operator is that it only looks neat when we need to perform one instruction. It’s better to use the if...else method when we need to do something complex.
The result of the operation can also be saved to a variable. In the example below, we save the result of a ternary expression to the result variable:
The result variable will keep the return value of the stay_home or the go_party method. In the example above, the result variable could be the number of drinks one drank: “If I stay home, I will watch Netflix and will drink from 0 to 1 beer; but if I go to the party, I will drink from 1 to 2 beers.” So, the result variable will just keep the number in our example, and the x variable will hold true or false, indicating if it is raining outside or not.
The two lines shown above can also be rewritten with the if...else clause: