Ruby unless Statement

The unless statement in Ruby is the opposite of the if statement. It runs a block of code only if a condition is false.

Here's a simple example of the unless statement. You can read the rest of the tutorial to learn more.

Example

age = 15

unless age >= 18
  puts "You are not old enough to vote."
end

# Output: You are not old enough to vote.

Here, age >= 18 is evaluated as false, so the condition fails, and the code inside the unless block is executed.


Syntax of the unless Statement

The syntax of the unless statement is:

unless condition
  # Code runs if the condition is false
end

Ruby unless With else

You can also use an else block with unless to handle both outcomes — when the condition is false and when it's true.

unless condition
  # Runs if condition is false
else
  # Runs if condition is true
end

Let's take a look at an example.

logged_in = true

unless logged_in
  puts "Please log in to continue."
else
  puts "Welcome back!"
end

Output

Welcome back!

In the above example, logged_in is true. So, the unless condition fails and the code inside the else block is executed.

Note: The unless statement doesn't support elsif. If you need to check multiple conditions, it's better to use if instead.


Ruby unless Modifier

Ruby allows you to write unless in a single line. This is called an unless modifier.

The syntax of the unless modifier is:

statement unless condition

For example,

logged_in = false

puts "Access denied" unless logged_in

The above line means, "Run the puts statement only if logged_in is false."


Frequently Asked Questions

What is the difference between the unless and the if statements?

In Ruby, both if and unless are used to control the flow of a program based on conditions. The key difference is:

  • if runs the code when the condition is true.
  • unless runs the code when the condition is false.

Example of an if statement:

is_logged_in = true

if is_logged_in
  puts "Welcome back!"
end

# Output: Welcome back!

Example of an unless statement:

is_logged_in = false

unless is_logged_in
  puts "Please log in."
end

# Output: Please log in.
When should we not use the unless statement?

While unless can improve readability in many cases, it may become confusing (especially when used with double negatives). For example:

unless !user_logged_in
  puts "You're logged out."
end

This reads as "unless not logged in," which can be hard to understand.

Although it's valid Ruby, using an if statement is usually clearer in such situations. The equivalent if version of the above code is:

if user_logged_in
  puts "You're logged out."
end

However, it makes no sense to print that the user is logged out when user_logged_in is true. To maintain logical consistency, consider one of the following fixes depending on your intent:

Fix 1: Rectify the Printed Message

Inform the user that they're logged in:

if user_logged_in
  puts "You're logged in."
end

Fix 2: Change the Variable Name to 'user_logged_out'

Keep the original message but make the condition match:

if user_logged_out
  puts "You're logged out."
end

Tip: If the condition includes a not (!), it's usually a sign that if might be a better fit than unless.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges