The Beginner's Guide to Ruby If & Else Statements

Today you'll learn exactly what a Ruby if statement is, how to write a conditional statement in Ruby & why is this so important!


How Do You Make Decisions in Ruby?

Like these:

  • “If the room is too cold turn on the heater”
  • “If we don’t have enough stock of this product then send an order to buy more”
  • “If this customer has been with us for more than 3 years then send him a thank you gift”

Things like that are what I mean by making decisions.

If something is true (the condition) then you can do something.

In Ruby, you do this using if statements:

stock = 10

if stock < 1
  puts "Sorry we are out of stock!"
end

Notice the syntax. It’s important to get it right.

The stock < 1 part is what we call a “condition”.

This is what needs to be true for the code inside the condition to work.

In plain English this is saying:

“If the value of stock is less than 1 then print the ‘out of stock’ message, otherwise do nothing.”

Types Of Conditions

In the last example I’m using the “less than” symbol <, but there are other symbols you can use for different meanings.

Here’s a table:

Symbol Meaning
< Less than
> Greater than
== Equals
!= Not equals
>= Greater OR equal to
<= Less OR equal to

Notice that we use two equal == symbols to mean equality!

One equals sign = in Ruby means “assignment”, make sure to use == when you want to find out if two things are the same.

If you don’t this right you won’t get the expected results.

Ruby Unless Statement

With an if statement you can check if something is true.

But when you want to check for the opposite “not true” (false) there is two things you can do.

You can reverse the value with !.

Code example:

if !condition
  # ...
end

Or you can use unless, which is like if, but it checks for “not true”:

unless condition
  # ...
end

Remember, using unless in Ruby is just the reverse of using if.

You’re saying “if this is not true, then do this…”.

The If Else Statement

You can also say “if this is NOT true then do this other thing”:

if stock < 1
  puts "Sorry we are out of stock!"
else
  puts "Thanks for your order!"
end

The else part is always optional, but it can help you write more advanced logic.

You can take this one step further & use an elsif statement:

if stock < 1
  puts "Sorry we are out of stock!"
elsif stock == 10
  puts "You get a special discount!"
else
  puts "Thanks for your order!"
end

With elsif you can say:

“If stock is less than 1 print this message, else if stock equals 10 print this special message, otherwise if none of these are true then print the thank you message.”

How to Use Multiple Conditions

If you’d like to write compound conditions, where you are checking if two things are true at the same time, then this section is for you.

You can do this by using the && (AND) operator:

if name == "David" && country == "UK"
  # ...
end

This is saying:

“If the name is equal to ‘David’ and country is equal to ‘UK’ then do something.”

You can also use the || (OR) operator:

if age == 10 || age == 20
end

This means:

“If the age is 10 or 20 then do something.”

Notice how these two operators (&&, ||) allow you to combine conditions, but they need to be proper conditions.

In other words, you CAN’T do this:

if age == 10 || 20
end

This is not valid.

You need a full condition on each side (age == 10 || age == 20).

Things to Watch Out For

Just before we end this lesson I would like to mention a few problems you may run into & what to do about them.

The first is about comparing strings.

When comparing two strings they must look exactly the same!

Including the “casing”.

This means that “hello” & “Hello” are different words.

You can solve this by making them as equal as possible:

name  = "David"
expected_name = "david"

if expected_name.downcase == name.downcase
  puts "Name is correct!"
end

The key here is the downcase method on name.

By making both strings downcase you can make sure they’ll match if they have the same content.

For example:

“David” becomes “david”, and “david” stays “david”.

Now both are “david” so you can compare them.

Special Symbols in Strings

Another problem you may come across related to arrays is “special symbols”.

These symbols are for things like new lines n, and the tab key t.

The problem is when you try to compare two strings that look the same, but they have one of these special symbols.

To see these special symbols you will need to use the p method:

name = gets
p name

Try this code, type something in, and you will notice that name contains the newline character (which is not normally visible with puts).

To remove this character you can use the chomp method.

name = gets.chomp
p name

Now the newline character (n) is gone, which allows you to compare strings correctly.

If Construct in One Line

It’s possible to write an if statement using just one line of code.

Like this:

puts 123 if 2.even?

Which is the same as:

if 2.even?
  puts 123
end

This is a shorthand version which can be useful if you have a simple condition.

Is There an Alternative?

If you have an if else expression there is also a shorthand for that.

It’s called the ternary operator:

40 > 100 ? "Greater than" : "Less than"

I have another article where you can learn more about how this works & learn about other useful Ruby operators.

Read that, then practice & review what you learned today.

Take notes. Practice. Repeat.

A very important component of learning is repetition, so do this & master Ruby if statements to make progress in your journey of becoming a great Ruby developer.

I know you can do it.

Summary

Conditions allow you to take decisions in your code, this is what makes your program “think”.

You also learned how to use the if statement & the else statement to handle different situations where you want to make decisions.

Finally, you learned about a few things to watch out for, like string “casing” & special symbols.

Now it’s your turn to take action!

Copyright RubyGuides.com