Table of contents
1.
Introduction
2.
Conditionals
3.
The ?: Operator in Ruby
3.1.
Syntax
3.2.
Example
4.
Multiple ternary operators
5.
Advantages of ?: operator in ruby
6.
Disadvantages of ?: operator in ruby
7.
Frequently asked questions
7.1.
How does a ternary operator work?
7.2.
What are other conditional structures?
7.3.
What benefit does a ternary operator offer over an if-else clause?
7.4.
Can the ternary operators be overloaded?
7.5.
Are the ternary operators faster than an "if" condition?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

The ?: Operator in ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a language of careful balance and is a perfect blend of five different languages  (Perl, Smalltalk, Eiffel, Ada, and Lisp). The ruby language provides various control structures to control and modify the execution of any statement. One of those control structures is conditionals. 

In this blog, we will learn about the ?: operator in ruby which is a conditional control structure. We will also learn its usage with various examples.

Ruby logo

Conditionals

The conditional is the most frequent type of control structure across all computer languages. This is a means of instructing the computer to only run certain code when a certain condition is met, or conditionally execute some code. The expression that makes up the condition must evaluate to a value other than false or nil in order for it to be satisfied.

Ruby offers a wide variety of conditional expressions. The subsections that follow provide descriptions of the syntactic options. We can use the approach that looks the most elegant for the task at hand when writing Ruby code.

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 ifif/elsenested 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 DatabasesNon- relational databasesMongoDBTop-100-SQL-problemsinterview-experienceIntroduction to ruby on railsDirectory Structure in RubyRuby on RailsRuby vs Python. You can also refer to the Official RubyRubyOfficial DocumentationRuby FAQRuby KoansRuby DocWhy Ruby?

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion

Live masterclass