ZetCode

Ruby unless Keyword

last modified April 27, 2025

This tutorial explains how to use Ruby's unless keyword. It serves as a negative conditional, executing code when a condition is false.

The unless keyword is Ruby's equivalent to "if not". It provides cleaner syntax for negative conditions. It can be used in statement modifiers too.

unless improves code readability when checking for negative conditions. It works with else but not elsif. Multiple conditions require logical operators.

Basic unless Example

This simple example demonstrates the fundamental usage of unless. It executes code when the condition evaluates to false.

basic_unless.rb
x = 5

unless x > 10
  puts "x is not greater than 10"
end

The code inside the unless block runs because the condition (x > 10) is false. This is equivalent to if !(x > 10) but more readable.

unless with else

unless can be paired with else to provide alternative execution paths. The else block runs when the condition is true.

unless_else.rb
logged_in = false

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

The first message prints because logged_in is false. If we set it to true, the else block would execute instead. This mirrors if-else logic.

unless Modifier

Ruby allows unless as a statement modifier for concise one-liners. The code executes unless the condition is true.

unless_modifier.rb
password = "secret"
puts "Access denied" unless password == "password123"

This prints "Access denied" because the password doesn't match. The modifier form is ideal for simple negative conditions at the end of statements.

unless with Multiple Conditions

Combine unless with logical operators for complex conditions. Parentheses help clarify evaluation order.

multiple_conditions.rb
age = 25
subscribed = false

unless age < 18 || subscribed
  puts "Please subscribe to access premium content"
end

The message displays because neither condition is true. The unless block runs when all combined conditions evaluate to false.

unless with Method Calls

unless works well with methods that return boolean values. It improves readability for negative checks.

method_calls.rb
def admin?(user)
  user[:role] == "admin"
end

user = { name: "John", role: "user" }

unless admin?(user)
  puts "Regular user privileges granted"
end

The code checks if the user isn't an admin. The negative condition reads more naturally with unless than with if !admin?(user).

unless for Nil Checks

A common Ruby idiom uses unless to handle nil values safely. This pattern prevents NoMethodError exceptions.

nil_checks.rb
def process_order(order)
  unless order.nil?
    puts "Processing order ##{order[:id]}"
  else
    puts "No order to process"
  end
end

order = nil
process_order(order)

The unless block safely handles the nil case. This defensive programming style is common in Ruby codebases.

unless in Loops

unless can control loop execution by checking continuation conditions. It stops the loop when the condition becomes true.

loop_control.rb
counter = 0

loop do
  puts "Counter: #{counter}"
  counter += 1
  break unless counter < 5
end

The loop continues while counter < 5 is false. When counter reaches 5, the condition becomes true and the loop breaks. This inverts typical if logic.

Source

Ruby Keywords Documentation

This tutorial covered Ruby's unless keyword with practical examples showing conditional execution, modifiers, and common patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Ruby tutorials.