Ruby if, then, elsif, and else Keywords
last modified October 18, 2026
This tutorial explains how to use Ruby's conditional statements with if,
then, elsif, and else keywords. These
constructs control program flow based on conditions.
The if keyword evaluates a condition and executes code when true. then separates the condition from the code block (optional in most cases).
elsif provides additional conditions to check when previous ones fail. else executes when all preceding conditions are false. These form the complete conditional structure in Ruby.
Only false and nil are falsy in Ruby. Every other
value, including 0 and the empty string, is truthy, so conditions
do not need to be strictly boolean.
Basic if Statement
This simple example demonstrates the most basic form of conditional execution. The code block runs only when the condition evaluates to true.
age = 20 if age >= 18 puts "You are an adult" end
The condition age >= 18 is evaluated first. Since 20 is greater
than or equal to 18, the message prints. Without the if statement,
the code would always execute.
if with else Clause
Adding an else clause provides an alternative execution path when
the condition fails. This covers all possible cases for the condition.
temperature = 15 if temperature > 25 puts "It's hot outside" else puts "It's not hot outside" end
Since 15 is not greater than 25, the else block executes. The
else clause ensures some code always runs, regardless of the
condition's result.
if with elsif Clauses
Multiple conditions can be chained using elsif. Ruby checks each
condition in order until one evaluates to true.
score = 85 if score >= 90 puts "Grade A" elsif score >= 80 puts "Grade B" elsif score >= 70 puts "Grade C" else puts "Grade D" end
The first test fails because 85 is not at least 90, so Ruby evaluates the next
condition. Since score >= 80 is true, the B branch
runs and the remaining branches are skipped. The else handles any
score below 70.
Using then Keyword
The then keyword separates the condition from the code block. It is
optional in normal multi-line conditionals; on a single line it separates the
condition from the body.
x = 10 if x == 10 then puts "x is 10" end # Multi-line version without then if x == 10 puts "x is 10" end
Both forms work identically. The then version is more compact for
simple conditions. For complex conditions or multiple statements, the multi-line
form is preferred for readability.
Nested if Statements
if statements can be nested inside other if statements
to create complex decision trees. Each level adds another condition to check.
age = 25
has_license = true
if age >= 18
if has_license
puts "You can drive"
else
puts "You need a license first"
end
else
puts "You're too young to drive"
end
The outer if checks age, while the inner one verifies license status.
Nested conditionals should be used sparingly as they can become hard to read.
Consider using logical operators (&&, ||) instead.
Conditional Assignment
In Ruby, if is an expression, so it returns the value of the branch
that executes. That value can be assigned directly to a variable.
status = if rand(2) == 1
"active"
else
"inactive"
end
puts "Account status: #{status}"
The entire if expression evaluates to either "active" or
"inactive", which is assigned to status. Because of
rand(2), the result changes from run to run. This pattern is common
when the result of the condition is the primary purpose.
Modifier if Form
Ruby offers a postfix if syntax for simple conditions. The code
executes only if the condition following it is true.
logged_in = true puts "Welcome back!" if logged_in # Equivalent to: if logged_in puts "Welcome back!" end
The modifier form is concise for single statements with simple conditions. For
complex conditions or multiple statements, the standard if form is
more appropriate. The modifier form doesn't support else clauses.
if vs unless
unless is the negated counterpart of if: it runs its
body when the condition is false. Use it for simple negative conditions, and
avoid unless ... else, which is harder to read.
logged_in = false unless logged_in puts "Please log in" end puts "Guest access" unless logged_in
Both forms print a message because logged_in is false. The modifier
form is equivalent to a single-statement unless block.
Source
Ruby Control Expressions Documentation
This tutorial covered Ruby's conditional statements with practical examples showing basic usage, branching, nesting, and special forms.
Author
List all Ruby tutorials.