Syntax for the Unless Conditional in Ruby
The unless conditional is a popular programming process for many rubyists. It's not required and many developers I know, even experienced ones, don't like it since it is a very different way of looking at conditionals. I'll leave it up to you to decide if you feel good about using it in your programs.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

The unless conditional is a popular programming process for many Rubyists. It's not required and many developers I know, even experienced ones, don't like it since it is a very different way of looking at conditionals. I'll leave it up to you to decide if you feel good about using it in your own programs. Either way you'll run into it in a large number of Ruby programs so it's important to at least understand the syntax.

Running Ruby Files

In this lesson, I'm going to use a regular Ruby file because I want you to get familiar with the different environments to write and execute Ruby code. As a quick review, to run Ruby code inside files you can follow these steps:

  1. Create a file with a text editor or IDE, make sure the file ends in .rb
  2. Open up the Unix terminal and make sure that you're in the same directory as the file that you created. If you're new to using the terminal you can save your file to the desktop and then direct your terminal to the desktop with the command cd ~/Desktop
  3. From here you can run the Ruby file with the following terminal command: ruby name_of_your_file.rb

Unless Conditional Code Example

In working through the unless conditional I've created a file called unless_syntax.rb

First, I'm creating an array for our unless conditional to work with.

players = ["Correa", "Carter", "Altuve"]

I want my code to print the values from the players array, but only if the array has values in it. To accomplish this I'm going to use the following code:

unless players.empty?
  players.each { |player| puts player }
end

To run this program, go to the terminal and type: ruby unless_syntax.rb

The output is:

large

Now, what happens if the array is empty?

Remove the values from the array, like this:

players = []

unless players.empty?
  players.each { |player| puts player }
end

Save the file and run it in the terminal, this is the output you will see:

large

The program doesn't print anything because this is following the rule that our unless conditional dictated. Now if you're a natural born skeptic like me you may say: "nothing got printed because nothing was in the array!". Fair enough, let's update the code to look like this:

players = []

unless players.empty?
  puts "I'm inside the unless statement"
  players.each { |player| puts player }
end

Now if you run this code you'll see that nothing gets printed out. Even our "I'm inside the unless statement" isn't shown. This is because with the way that conditionals work is that they check to see if condition is met, and if it's not (like our players array being empty) it will skip all of the code inside of the unless block.

If you read the code like spoken language, it should read like:

unless the players array is empty, print it out.

In the second run, the players array was empty, so the program does not process the code in the block.

Now the unless conditional can be challenging for some developers to get their mind around. Essentially unless is the exact opposite as if. For example, we could change our program to say: if !players.empty? and it would work the same as our current unless based code.

You can use either option depending on what you prefer. Personally, there are a number of circumstances when unless makes sense to use so I will utilize it in those cases.

In the preceding example we used unless with a code block. However I'll typically use unless on a single line of code. For example we can refactor our code to read like this:

players.each { |player| puts player } unless players.empty?

This is one of the nicest things about Ruby because you can actually read this one line of code like an spoken sentence:

Iterate through each value in the players array and print each player unless the array is empty.

How simple is that?! Not all programming languages offer this type of convenience. I can also do the same thing with the if statement too (assuming I use the ! process to get the opposite value):

players.each { |player| puts player } if !players.empty?

I hope this helps you to appreciate the simplicity of Ruby and that you have the flexibility to utilize multiple syntax options to accomplish the same behavior.