100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Ruby

Conditional Statements in Ruby

How to control program flow in Ruby with if/elsif/else, unless, ternary expressions, and modifier conditionals, plus Ruby's truthiness rules.

Control FlowBeginner8 min readJul 9, 2026
Analogies

Conditional Statements in Ruby

Conditional statements let a program branch its execution based on whether an expression evaluates as truthy or falsy. Ruby provides several syntactic forms for conditionals—full if/elsif/else/end blocks, the negated unless, compact modifier forms, and ternary expressions—giving developers flexibility to match the shape of the conditional to the complexity of the logic. Crucially, in Ruby, if and unless are expressions, not just statements: they evaluate to a value, which can be assigned directly to a variable.

🏏

Cricket analogy: A third umpire's review isn't just a yes/no gate, the decision itself, out, becomes the value fed straight into the scoreboard update, the way Ruby's if evaluates to a usable value, not just a branch.

if, elsif, else, and unless

A standard conditional begins with if, can include any number of elsif clauses, an optional else, and closes with end. unless is the logical inverse of if, running its body only when the condition is falsy; it reads naturally for negative conditions ('unless the user is an admin') but should be avoided for anything beyond simple negation, since unless...else quickly becomes confusing to read. Because if/unless are expressions, the last evaluated line inside the matching branch becomes the whole expression's return value.

🏏

Cricket analogy: A DRS review flowchart runs if no-ball, elsif caught behind, else not out in sequence, checking each condition only until one matches, just as Ruby's if/elsif/else short-circuits at the first true branch.

ruby
age = 20

if age < 13
  category = "child"
elsif age < 20
  category = "teen"
else
  category = "adult"
end

# Since if is an expression, this can be written more idiomatically:
category = if age < 13
             "child"
           elsif age < 20
             "teen"
           else
             "adult"
           end

puts "Welcome!" unless age < 18

Modifier Form and Ternary Expressions

For single-statement conditionals, Ruby offers a trailing modifier form: statement if condition or statement unless condition, which reads close to natural English and avoids the overhead of a full block for simple guard clauses. For choosing between two values based on a condition, the ternary operator condition ? value_if_true : value_if_false is idiomatic when both branches are short expressions, though it should be avoided for complex or nested logic where a full if/else block reads more clearly.

🏏

Cricket analogy: Declare if the lead exceeds 300 is a quick guard-clause call a captain makes in one breath, while a full sit-down team meeting, a full if/else block, is reserved for genuinely complex declaration scenarios.

ruby
puts "Discount applied" if cart_total > 100
raise ArgumentError, "age required" unless age

# Ternary — best for short, simple branches
label = age >= 18 ? "adult" : "minor"

# Avoid nested ternaries; prefer if/elsif for multi-branch logic
# BAD: status = age < 13 ? "child" : age < 20 ? "teen" : "adult"

Recall that in Ruby, only false and nil are falsy — so if 0 and if "" both evaluate their body, unlike in JavaScript or Python. This is a frequent source of confusion for developers coming from those languages.

Avoid unless...else; because unless already inverts the logic, adding an else clause forces readers to mentally negate twice. Rewriting as a plain if...else with the branches swapped is almost always clearer.

  • Ruby conditionals use if/elsif/else/end; unless is the inverse of if, running its body when the condition is falsy.
  • if and unless are expressions in Ruby and return the value of the last executed line, so they can be assigned to a variable.
  • The modifier form (statement if condition) is idiomatic for simple, single-line guard clauses.
  • The ternary operator cond ? a : b is best reserved for short, non-nested branches.
  • Only false and nil are falsy in Ruby; 0 and empty strings/arrays are truthy, unlike many other languages.
  • Avoid unless...else, since double negation makes conditions harder to read; use if...else instead.

Practice what you learned

Was this page helpful?

Topics covered

#Ruby#RubyStudyNotes#Programming#ConditionalStatementsInRuby#Conditional#Statements#Elsif#Else#StudyNotes#SkillVeris