The ?: Operator in Ruby
The ?: operator in ruby is a conditional operator alternative to the “if/else” conditions. It is a ternary operator and is made of three parts. These parts include one conditional statement and two possible outcomes.
In other words, the ternary operator gives us a way to write the if/else expression in just one line of code. Now, let’s see the syntax for the same.
Syntax
condition ? trueStatementExecuted : falseStatementExecuted

You can also try this code with Online Ruby Compiler
Run Code
The condition, or the thing we want to verify to see if it's true or not, comes first in a ternary operator. The next character is a question mark (?). The syntax includes that! Ruby learns that we’re building a ternary operator from this.
Next, if the condition is true then the statement just after the question mark (?) and before the (:) mark (i.e. trueStatementExecuted), will be executed. But if the condition is false then the statement after the (:) mark (i.e. falseStatementExecuted) will be executed.
Example
Now, that we have learnt the syntax of the ?: operator in ruby let’s understand this better with an example.
totalMarks = 85
status = totalMarks > 33 ? "Pass!" : "Fail!"
puts status

You can also try this code with Online Ruby Compiler
Run Code
In the above example, with the help of the ?: operator in ruby we are just checking if the status of the student is pass or fail. And finally, we are printing the status of the student.
Output
Pass
Multiple ternary operators
We can use the nested ternary operators just like the nested if/else. Let’s modify the above example to check for one more case. This modification will require the use of multiple ?: operators in the code.
totalMarks = 85
status = totalMarks >80 ? 'Distinction!' : totalMarks > 33 ? 'Pass!' : 'Fail!';
puts status

You can also try this code with Online Ruby Compiler
Run Code
In the above example, we have used two ?: operators to check if the marks are above 80 to qualify for distinction or else if it is greater than 33 to pass otherwise the student is failing.
Output
Distinction!
Advantages of ?: operator in ruby
In this section, we will learn about the advantages of the ternary operators in ruby over the if/else conditional structures.
- It is short, so easy and short to write.
- It improves the readability of the written code.
- The code becomes straightforward.
Disadvantages of ?: operator in ruby
Now that we have seen the usage and advantages of the ternary operators in ruby let’s also discuss its disadvantages. We can find some crucial disadvantages of the ?: operators in ruby listed below:
- Nested multiple uses of ternary operators reduce readability.
- The complex use of the ternary operator makes it difficult to debug.
- Operator precedence always makes the output of the ternary operator wrong if any higher precedence operator is used in the statement or condition.
Frequently asked questions
How does a ternary operator work?
The ternary operator takes three arguments: The first is the conditional argument. The second is the result when the condition is true. The third is the result when the condition is false.
What are other conditional structures?
Some other conditional structures are also alternatives to the ternary operators in ruby are if, if/else, nested if/else, etc.
What benefit does a ternary operator offer over an if-else clause?
The ternary operator alters the overall appearance and length of the available code while serving the same purpose as an if/else condition. The ternary operator greatly simplifies coding and gives it a much more polished, algorithmic appearance.
Can the ternary operators be overloaded?
The ternary operators are largely used for flow control, despite the fact that it is technically an operator; hence, overloading it would be more similar to overloading if or while than it would be to overloading most other operators.
Are the ternary operators faster than an "if" condition?
No, there is no major difference in the performance and efficiency of both the ternary operators and the “if” conditions.
Conclusion
In this article, we have discussed the ternary (?:) operator in ruby along with its usage, advantages and disadvantages. We have also seen various examples to understand the ternary operator better.
We hope you enjoyed this article on Ruby. To see more please refer to these useful links: Operational Databases, Non- relational databases, MongoDB, Top-100-SQL-problems, interview-experience, Introduction to ruby on rails, Directory Structure in Ruby, Ruby on Rails, Ruby vs Python. You can also refer to the Official Ruby, Ruby, Official Documentation, Ruby FAQ, Ruby Koans, Ruby Doc, Why Ruby?
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!
