A article about the unless keyword in Ruby. A bit misleading at first glance, but very readable finally. Let's dive.

· Ruby  · 2 min read

Ruby unless

A article about the unless keyword in Ruby. A bit misleading at first glance, but very readable finally. Let's dive.

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.

Share:
Back to Blog

Related Posts

View All Posts »
Ruby constants

Ruby constants

Constant are not so often used in Ruby, however, it's always good to know how they work when the need arises. Constants are simply variables that are not meant to be changed.

How to create tons of Rails apps

How to create tons of Rails apps

Yes, I already now about the rails new command. But what if you want better defaults, like an existing controller, an existing view, or another way to to launch the Procfile?

Rails new options

Rails new options

Let's see the official way to create Rails application, as well as the minimalistic option.