When you’re programming in Ruby, you may want to run certain code only if a condition is met. For example, you may want to stop a customer from buying a product if there is no stock left in inventory, or only allow a customer to proceed with a purchase if they have filled out all the relevant forms.
In order to perform these actions, we need to employ the use of an
if statement. In simple terms, if statements allow Ruby programs to make decisions based on certain criteria. If a condition is met, then the program will do something.
In this tutorial, we are going to break down the basics of
if statements in Ruby, discuss the types of conditions you can use, and explore
if else statements.
Ruby If Statement
If statements can be used to allow a program to make decisions based on a certain set of criteria. For example, you may want to give a customer a discount if they are a senior citizen, or if they have a coupon listed on their account.
Here is the syntax for an if statement in Ruby:
if conditions_are_met // Run code End
Let’s use an example to illustrate how if statements work. Let’s say we have a user who is trying to buy a ticket on our online store, and we need to verify if they are 16 or older. Here’s an example of an if statement that will accomplish this goal:
user_age = 17 if user_age < 16 puts "You are not old enough to use this service." End
Our above code returns nothing because our user’s age is 17. But if our user was under the age of 16, we would see this message:
You are not old enough to use this service.
If Else Statement
With an if statement, we only execute code when our requirements are met. However, we may want something else to happen if our conditions are not met.
For example, let’s say that we want to present a user with a message if they missed filling out the address form field when they were making a purchase. We also want to present them with a message if the form was filled out correctly, so they know their purchase will go through.
We can use the
if else statement to implement this in our code. The
else statement is written after the
if statement and has no conditions. Here is the syntax for an else block:
if conditions_are_met // Run code else // Run other code End
Let’s use the same example as we used above to illustrate an
if statement, and add in a message that appears if a user is 16 or older:
user_age = 17 if user_age < 16 puts "You are not old enough to use this service." else puts "Your purchase is being processed." End
The above code first defines our user’s age, which is 17 in this case. Then, it runs an
if statement to evaluate whether the user age is less than 16 conditional is true. Since our user is over the age of 16, the program runs the
else code. In this case, our code returns the following:
Your purchase is being processed.
We can take this one step further by using an
elsif statement. This statement gives us the ability to check for another set of criteria if our first statement evaluates to false. For example, we may be operating a movie theater and offer different rates depending on the age of the customer.
Here’s an example of an
elsif statement that checks a customer’s age and returns the price they should pay for a ticket:
customer_age = 17 ticket_price = 0 if customer_age <= 16 ticket_price = 10 elsif customer_age > 16 && customer_age < 65 ticket_price = 15 elsif customer_age > 65 ticket_price = 10 end puts ticket_price
There is a lot going on here, so let’s break it down. On the first lines, we define our customer’s age as
17, and the ticket price as
. Then, our program executes an if statement to check the age of the customer and set the ticket price accordingly.
If our customer is 16 or under, they pay $10 for their ticket; if our customer is aged over 16 and under 65, they’ll pay $15; if our customer is over the age of 65, they’ll pay $10.
In this case, our customer is 17, which means our code will return the following:
If you want to check if two things are true at the same time, you can use multiple conditions. In the above example, we illustrated how an
if statement can be given multiple conditions by checking if a customer’s age was between 16 and 65.
In Ruby, we use the
AND) operator to separate the conditions we want to check are true. Here’s an example of an if statement with multiple conditions:
if user_discount == true && age < 5 // Run code End
We can use the
&& operator as many times as we want to check if a certain statement evaluates to be true.
On the other hand, if we want to check if either one of two or more statements are true, we can use the
OR) operator. This operator would be useful if, for example, we want to run code if a user is either a
platinum customer. Here’s an example of the
|| operator in action:
loyalty_plan = "Gold" discount = 0 if loyalty_plan == "Gold" || loyalty_plan == "Platinum" discount = 10 end print discount
Because our user is on the
Gold loyalty plan, our code returns the following:
But if our user was not on the
Platinum loyalty plan, our code would return
In the if statement we defined earlier in the tutorial, we checked if a user’s age was 16 or greater, but there are other symbols you can use in an if statement. We call these conditional statements or conditional operators.
Here is a list of the conditions you can use in your if statements:
|!=||Not equal to|
|>=||Greater than or equal to|
|<=||Less than or equal to|
If statements can be used in the Ruby programming language to evaluate whether a statement is true and run code if that statement is true. They can be useful if you have specific code that should only be run when a set of conditions are met. For example, you may want to allow a user to submit a form only if they are logged in.
In this tutorial, we have discussed the basics of
if statements in Ruby. We have also explored how you can use
elseif to customize your if statements based on multiple possible outcomes.
Now you’re ready to use
if statements like a Ruby expert!
Ruby is used by professional developers around the world for a variety of purposes. If you’re curious about how learning Ruby can help you break into your dream career in tech, download the free Career Karma app and talk with one of our coaches!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more