ZetCode

Ruby OR Keyword

last modified October 18, 2026

This tutorial explains how to use Ruby's or keyword. It is a short-circuit logical operator: it returns its first truthy operand, or its last operand if none is truthy.

The or keyword has lower precedence than ||. In Ruby, only nil and false are falsey; every other value is truthy.

Because of that low precedence, or is typically used for control flow, while || is preferred for boolean logic and assignments.

Basic OR Operation

This example demonstrates the fundamental behavior of the or operator. It evaluates to a truthy value when at least one operand is truthy.

basic_or.rb
a = true
b = false

if a or b
  puts "At least one is true"
else
  puts "Both are false"
end

The code checks if either a or b is true. Since a is truthy, the first message prints. The or operator short-circuits: it does not evaluate b once it has a truthy operand.

OR for Default Values

The or operator can provide default values when a variable might be nil or false. Because or has lower precedence than assignment, parentheses are required when assigning the result.

default_values.rb
name = nil
display_name = (name or "Anonymous")

puts "Welcome, #{display_name}"

age = false
user_age = (age or 30)

puts "Age: #{user_age}"

When name is nil, or returns the default "Anonymous" string. Without the parentheses, display_name = name or "Anonymous" would assign nil to display_name and then evaluate the default. The same applies to the false age value, where the parenthesized version yields 30.

For default values, || is the more common choice because it binds more tightly than assignment and needs no parentheses.

OR in Conditional Statements

or can combine multiple conditions in if statements. It's often used to check for alternative acceptable conditions.

conditional.rb
weather = "rainy"
temperature = 15

if weather == "sunny" or temperature > 20
  puts "Good weather for a walk"
else
  puts "Better stay inside"
end

The condition checks for either sunny weather or warm temperature. Since neither is true, the else clause executes. The or makes the condition more flexible.

OR vs || Difference

While similar, or and || have different precedence. This example shows how it affects evaluation order.

precedence.rb
x = false || true   # => true
y = false or true   # => false

puts "x: #{x}, y: #{y}"

a = 1 || 2          # => 1
b = 1 or 2          # => 1

puts "a: #{a}, b: #{b}"

The || operator binds more tightly than assignment, so x = false || true assigns true. The or operator binds less tightly, so y = false or true assigns false and then evaluates the discarded true.

OR for Fallback Values

or can provide a fallback when a method returns a falsey value. Parentheses are needed for the same precedence reason as above.

fallback_value.rb
def first_method
  nil
end

def second_method
  "fallback value"
end

result = (first_method or second_method)
puts result

When first_method returns nil, or evaluates second_method and returns "fallback value". Without the parentheses, result would be nil. For fallback values, || is usually the clearer choice.

OR for Error Handling

The or operator can handle potential errors by providing alternative execution paths when operations fail.

error_handling.rb
def load_config
  # Simulate failed config load
  nil
end

config = load_config or raise "Failed to load configuration"

puts "Config loaded: #{config}"

If load_config returns nil, the or operator executes the raise statement. Assignment binds more tightly than or, so config receives the value first and the fallback runs only when that value is falsey. This provides clean error handling without nested conditionals.

OR in Loops

or can control loop execution by combining multiple stopping conditions. This makes loop control more flexible.

loop_control.rb
count = 0
max_attempts = 3
success = false

until count >= max_attempts or success
  puts "Attempt #{count + 1}"
  success = rand > 0.7
  count += 1
end

puts success ? "Succeeded!" : "Failed after #{count} attempts"

The loop continues until either the attempt limit is reached or success is achieved. In other words, it runs while attempts remain and success is still false. The or combines the two stopping conditions.

Source

Ruby Keywords Documentation

This tutorial covered Ruby's or operator with practical examples showing its use in conditionals, assignments, error handling, and loops.

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.