The unless conditional code example

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

First, I create 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 as follows:

Now, what happens if the array is empty?

Remove the values from the array as shown in the following code:

players = [] 

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

Save the file and run it in the Terminal. The following is the output that you will see:

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. Even "I'm inside the unless statement" isn't shown. This is because the way that conditionals work is that they check to see if the 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 the following:

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 of if. For example, we could change our program to say if !players.empty?, and it would work the same as our current code which is based on unless.

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 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 the following:

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 a 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 the flexibility it gives you to utilize multiple syntax options to accomplish the same behavior.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138