9. Conditional Expressions – Part I

Now we know how to create variables and use interpolation to insert them into strings. Those are good starts to a program, but there’s still something missing. A good program doesn’t just evaluate data: it uses that data to make decisions on what it should or shouldn’t do.Let’s add some logic to our programs! You remember what a conditional statement is, right? In this lesson, we’ll look at how to use them in Ruby.

If You Please

Ruby keeps things straightforward: an if statement is written using the if keyword. An if block – the code that an if statement can run – starts with the if statement and ends with the end keyword. Let’s see an example:

name = "Bridge"  if name == "Bridge"   puts "Hello Bridge" end 

Do you remember the equality operator: ==? We use this with the if keyword to say **if* the stuff on the left side of == is the same as the stuff on the right side, then execute all of the following code until you reach the end keyword.* This is called a conditional statement.

In the example above, we’re saying **if* the value stored in the variable name is the same as the string “Bridge”, then display the words “Hello Bridge” on the screen.* It can’t get any easier than that, right?

It’s Ruby, Let’s Simplify

It can get easier than that. Ruby is super expressive – it doesn’t want us to write more code than needed for the interpreter to understand what we want to do. This means that we can simplify the above example even more.

If your if block only has one statement, you can just place the condition at the end of that statement without formally writing the whole block. Let’s take a look:

name = "Bridge" puts "Hello Bridge" if name == "Bridge" 

If you run this code, you’ll get the exact same result as the first example – we’re just changing the way we tell Ruby what we want to do. This time, we’re saying display “Hello Bridge” on the screen only if the name is Bridge.

If your if block contains more than one statement, then you’ll have to use a regular if … end block:

name = "Bridge" if name == "Bridge"   puts "Hello Bridge"   puts "Nice to see you here, Bridge" end 

Quick Recap

Conditional statements are a huge part of programming – without them, we wouldn’t be able to interact with our programs. This is important stuff, so we want to make sure that you understand it all.

See if you can predict the output of the program below – write down your prediction and test your answer by running the code on your computer. If you don’t quite understand the result, head over to The Forums and ask for help before proceeding any further. You’ll be using this concept a lot in your future programs, so you need to make sure you understand it now!

firstname = "Rob" lastname = "Stark"  if firstname == "Jaime"   puts "Hey Jaime" end  puts "How are you, #{firstname}?"  puts "Winter is coming." if lastname == "Stark" 

How did you do? Did your prediction match the output of the program? Great! We’ve got a lot more to see, so let’s keep moving!

A Keyword For The Rest

Let’s say you want to display Use the Force, Luke if the name is Luke – just use an if statement, right? Easy!

But what if you want to display something else if the name is not Luke? We could just write another if statement, but there must be something else we can do. How else can we display something else on the screen?

We’re not very subtle, are we? The else keyword is here to help us do something else if the if statement is not true:

name = "TK421"  if name == "Luke"   puts "Use the Force, Luke" else   puts "These are not the droids you are looking for" end 

This is an if block that contains an else clause. We’re simply telling Ruby if the if statement evaluates to true, run the code in this block; otherwise, run the code in the else block.

Not Equal

You should already know about the not equal relational operator from our previous lessons. Ruby uses the same operator that we used in our pseudocode: !=.

So another way to write the above example would be like this:

name = "TK421"  if name != "Luke"   puts "These are not the droids you are looking for" else   puts "Use the Force, Luke"       end 

This time we’re saying if the name is *not** Luke, display the words “These are not the droids you are looking for.” Otherwise, display the words “Use the Force, Luke.”*

Equal and Not Equal are two very important foundations in any program. Make sure that you really understand these concepts!

When One Condition Is Not Enough

Adding logic to our programs greatly expands what we can make them do. But sometimes we need to test more than one condition. We may need to check two or more conditions and if they’re both true or if only one of them true.

That’s where And (&&) and Or (||) come in. You should remember these from before.

We could test two or more conditions by using nested if statements:

username = "Dumbledore" password = "Sherbet Lemon"  if username == "Dumbledore"   if password == "Sherbet Lemon"     puts "Welcome, Albus."   end end 

Here we’re saying if the username is “Dumbledore”, then run this code: if the password is “Sherbet Lemon”, then run this code: display “Welcome, Albus” on the screen.

That’s not easy to read or understand, is it? It’s much easier to use the and keyword (represented by two ampersands, &&, in Ruby) to check both conditions at the same time:

username = "Dumbledore" password = "Sherbet Lemon"  if username == "Dumbledore" && password == "Sherbet Lemon"   puts "Welcome, Albus." end 

Now we’re saying if the username is “Dumbledore” *and** the password is “Sherbet Lemon”, then display “Welcome Albus” on the screen*.

That’s much better: now we can easily read the code and understand what it does!

If we want to test several conditions to see if at least one of them evaluates to true, we can use the or keyword (represented by two pipe characters, ||, in Ruby) to check all conditions at the same time:

kiwis = 0 apples = 7 oranges = 0  if kiwis > 0 || apples > 0 || oranges > 0   puts "You have at least one fruit" else   puts "You don't have any fruits" end 

Here we’re saying if kiwis is more than 0, *or** if apples is more than 0, or if oranges is more than 0, then display “You have at least one fruit” on the screen. Otherwise, display “You don’t have any fruits” on the screen”*. Only one of the three conditions needs to be true for the if block to run.

Trust us, you don’t want to see what that example looks like using nested if statements (but you could always try to write it to see for yourself)!

Again, This Is Ruby

Ruby has a special keyword that you won’t see in many other languages: the unless keyword. It’s the exact opposite of the if keyword – it means if not. Here’s an example:

name = "Joey" puts "These are not the droids you are looking for" unless name == "Luke" 

Using the unless keyword can be tricky if you want to keep your code easy to read and understand. There are a few rules you’ll want to follow:

1. **Don't** use more than one logical condition. 2. **Don't** use it with a negative statement - `unless` is already negative. 3. **Never** use an `unless` statement with an `else` clause! 

You can read a quick discussion about the unless keyword on Signal v. Noise, the blog from the folks at Basecamp.


In this lesson we covered basic conditional operations in Ruby. You can now tell the interpreter to run a set of statements only if one or more conditions are true – and what to do if those conditions aren’t true.

In the next lesson, we’ll dig deeper into conditional operations and see even more useful ways to build good programs!

Important Words

  • Conditional operation
  • if
  • end
  • else
  • unless

Write a Ruby program that outputs either “Good morning”, “Good afternoon”, “Good evening”, or “Hello” based on the value of a time_of_day variable that can hold a string like "morning".

Once your program is finished, post it in the Forums so we can make sure you understand the basics of conditional operations in Ruby.

We can’t wait to see what you make!

Share to friends