Ruby

If Conditions in Ruby

Decision-making is a base factor in programming and human life. They allow us to perform certain actions when a condition fits a specific scope.

In this tutorial, we will focus on how to implement condition statements in Ruby programming language. Using Ruby conditionals, we can check for a specific condition and take a specified action based on the result.

The simplest way to make a single decision in Ruby is to implement an if statement.

Ruby If Statements

The if statement takes a condition and evaluates if it’s true or false. If true, it performs a decision.

The general syntax for a Ruby if statement is:

if condition
        // do this
end

Let us use an example to express how to implement the above syntax. For example, suppose we only sell movie tickets to a person of 18 years and above, we can do the following:

age = 20
if age >= 18
   print "Movie tickets are 10 bucks!"
end

In our example, the code returns the statement:

“Movie tickets are 10 bucks”We start by defining a variable that stores age. Next, we implement an if statement that checks if the age is greater than or equal to 18. If true, we sell the movie ticket; if not, do nothing.

Ruby Conditionals

In the above example, you will notice that we use the >= operator to check for the conditions. Ruby provides you with a few conditional operators to evaluate the various conditions. These operators include:

Operator Symbol Functionality
A greater than operator checks if the value or expression on the left is greater than the one on the right
< A less than operator checks if the value or expression on the left is less than one on the right
== An equal operator checks if the sets of values are equal
>=  Greater than or equal to
<= Less than or equal to
!=  Not equal and checks if the value on the left is not equal to one on the right

Negating a Condition

In Ruby, we can negate a condition by appending the exclamation (!) before the condition. That will reverse the result of the condition.

For example:

i = 10
if !(i < 3)
   print "Hi, I am negated!"
end

We start by defining the value of i as 10. Next, we check if 10 is less than 3, which evaluates to false. However, due to the (!), the condition is turned to true, leading to the printing of the string.

Ruby If Else statement

To implement multiple logic based on different conditions, we can use the If Else statements. In this case, if the condition is not true, do this.

Let us take the movie tickets example above. If the user is not 18 years and above and we do not do something, this can confuse the end-user.

Let us implement logic to tell the user they need to be at least 18 years of age.

age = 20
if age >= 18
   print "Movie tickets are 10 bucks!"
else
   print "You have to be at least 18 years of age!"
end

If we run the code above, we should get:

> ruby conditionals.rb
"Movie tickets are 10 bucks!"

Next, let us switch the age to be less than 18:

age = 17
if age >= 18
   print "Movie tickets are 10 bucks!"
else
   print "You have to be at least 18 years of age!"
end

Once we run the code, we should get the statement:

> ruby conditionals.rb
"You have to be at least 18 years of age!"

Ruby If…Elsif…Else Statements

Up to this point, we have only checked for two statements. If the age is greater than or equal to 18, sell a movie ticket, or else deny it. However, what if we want to implement different pricing for various age brackets?

In this case, we can use if..elsif…else statements. The general syntax for that is:

if (condition)
        # do
elsif condition2
        # do
elsif condition3
        # do
elsif conditionN
        # do
else
        #do

Let us implement the above logic in our movie ticket example. In this example, we will have four age brackets as shown in the code below:

Input “Enter your age: ”

age = gets
age = age.to_i
if age <= 17
   puts "You are under age to purchase a ticket"
elsif age > 18 && age <= 24
   puts "Tickets are 10 bucks!"
elsif age > 24 && age < 40
   puts "Tickets are 15 bucks!"
else
   puts "Tickets are 20 bucks!"
end

In the above example, we start by asking the user for their age. Next, we convert the input into an integer to perform logical comparisons on the value.

Finally, we create age brackets evaluating the age and their tickets prices.

Here is an example output:

Enter your age:
20
Tickets are 10 bucks!
Enter your age:
5
You are under age to purchase a ticket
Enter your age:
50
Tickets are 20 bucks!

Ruby If One-Liners

If we need to only check for one condition in Ruby, we can implement a one-line format as:

age = 18
puts "Tickets are 10 bucks" if age >= 18

We can express the above as, “If the age is greater than or equal to 18, print the following.”

Conclusion

In this guide, we discussed how to implement if conditions in Ruby.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list