DEV Community

Cover image for Ruby, the "unless" keyword
David Boureau
David Boureau

Posted on • Originally published at bootrails.com

Ruby, the "unless" keyword

Article originally posted here : https://bootrails.com/blog/ruby-unless

You are probably familiar with the "if else" control flow but there are certain situations where there may be a visually better way to write these statements. Here we will discuss how you can achieve this via the unless keyword in Ruby and the different ways you can use it.

Unless you already know...

When we use an if statement we want to check whether a condition is true or not. If it is true, you execute a block of code. If it is false, the code does not get executed. Simply put if checks for a truthy value. Like many other languages.

You can think of unless as the exact opposite of this. unless can replace the if keyword but it will check for a falsy value and execute the code if the condition it is checking is false.

Syntactically this is shown below:

puts "Passed" if score > 50  
Enter fullscreen mode Exit fullscreen mode
if score > 50  
  puts "Passed"  
end  
Enter fullscreen mode Exit fullscreen mode

Becomes

puts "Passed" unless score < 50  

unless score < 50  
  puts "Passed"  
end  
Enter fullscreen mode Exit fullscreen mode

As you can see the only difference is that you now need to change the condition. So unless is just the exact opposite of if.

You might be wondering why Ruby gives you a keyword for this specifically. Well writing your control flow this way will improve the visual appeal of the code as you will no longer need to flip your conditions around using the not operator in more complex conditions.

Samples:

# okay, understandable, but your eye may miss the "!"  
do_something if !some_condition  

# okay, readable, understandable, but "not" is not always liked by Ruby developers  
do_something if not some_condition  

# okay, readable, understandable  
do_something unless some_condition  
Enter fullscreen mode Exit fullscreen mode

As a modifier

You may have noticed in the last example that unless can also be used as a modifier similar to if.

In this sample:

unless score < 50  
  puts "Passed"  
end  
Enter fullscreen mode Exit fullscreen mode

Becomes

puts "Passed" unless score < 50  
Enter fullscreen mode Exit fullscreen mode

The left-hand side behaves as a then condition (code that will be executed) and the right-hand side behaves as a test condition.

Notes

Even though it is completely possible to use else in the unless statement it is suggested in best practices that you should avoid doing this and prefer to use the if keyword when you need to have an else condition. This is because using else with unless can create confusing control flows to read. For example:

# works, but not advised  
unless passed?  
  puts 'failed'  
else  
  puts 'passed'  
end  
Enter fullscreen mode Exit fullscreen mode

Personal thoughts

At the beginning, unless is clearly counter-intuitive. It took me some time before to use it daily without additional headaches. This keyword however is a good example of what Ruby is trying to achieve : increase readability.

Top comments (1)

Collapse
 
franklinyu profile image
Franklin Yu

Nitpick: the negation of score > 50 is actually score <= 50.