Image
#ruby
Posted June 10, 2021 ‐  3 min read

Ruby : The "unless" keyword

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
if score > 50
  puts "Passed"
end

Becomes

puts "Passed" unless score < 50

unless score < 50
  puts "Passed"
end

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

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

Becomes


puts "Passed"  unless score < 50

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

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.

Help needed 😊

If you enjoyed the article, you can :

  • Share the article on Twitter , or LinkedIn , or Reddit , it will stimulate the writing effort, thanks !
  • Subscribe to the newsletter, you'll be warned each time a new Rails tutorial is released : we start from the rails new command to fully understand a new concept.
  • Subscribe to the Bootrails Beta, it's a tool to launch new Rails apps.

Thanks to all,

David