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.
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 < 18Modifier 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.
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 : bis 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
1. In Ruby, what does the following return: `x = if true then "yes" else "no" end`?
2. What does `unless condition` execute?
3. Which values are falsy inside an `if` condition in Ruby?
4. Which conditional form is best suited for a simple single-line guard clause like raising an error if a variable is missing?
5. Why is `unless...else` generally discouraged in Ruby style guides?
Was this page helpful?
You May Also Like
Operators in Ruby
A tour of Ruby's arithmetic, comparison, logical, assignment, and range operators, including the fact that most operators are actually method calls.
Case Expressions
How Ruby's case/when expression uses the triple-equals operator for flexible pattern matching against classes, ranges, regexes, and values.
Loops in Ruby
A tour of Ruby's repetition constructs — while, until, for, loop, and the more idiomatic times/each iterators — with guidance on which one to reach for.
Variables and Data Types
Learn how Ruby variables work, its dynamic typing model, and the core built-in data types: integers, floats, strings, symbols, booleans, and nil.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics