if, unless, ternary
Branching basics.
if, unless, ternary is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
Branching Basics
Programs need to make decisions. In Ruby, the if keyword runs a block of code only when a condition is truthy.
An if expression evaluates a condition and, if it holds, runs the code between if and end.
age = 20
if age >= 18
puts "You are an adult"
endif / else
Add an else branch to handle the case where the condition is false. Exactly one branch runs.
age = 12
if age >= 18
puts "Adult"
else
puts "Minor"
endelsif Chains
Use elsif to test multiple conditions in order. Ruby checks them top to bottom and runs the first match.
- Note the spelling:
elsif, notelseiforelse if.
score = 75
if score >= 90
puts "A"
elsif score >= 70
puts "B"
elsif score >= 50
puts "C"
else
puts "F"
endif Is an Expression
In Ruby almost everything is an expression that returns a value. An if returns the value of the branch that ran, so you can assign it to a variable.
age = 20
status = if age >= 18
"adult"
else
"minor"
end
puts statusModifier if
For a single short statement, put if at the end of the line. This reads almost like English and is very common in idiomatic Ruby.
temperature = 35
puts "It is hot!" if temperature > 30Meet unless
unless is the opposite of if: it runs the body when the condition is false. Think of unless x as if !x.
logged_in = false
unless logged_in
puts "Please log in"
endModifier unless
Like if, unless also works as a trailing modifier for one-liners.
stock = 0
puts "Out of stock" unless stock > 0Avoid unless with else
unless can take an else, but it quickly becomes confusing to read. Prefer a plain if when you need both branches.
active = false
unless active
puts "Inactive"
else
puts "Active"
end
# Clearer as a normal if/elseThe Ternary Operator
The ternary operator is a compact if/else for a single value. Syntax: condition ? value_if_true : value_if_false.
age = 20
label = age >= 18 ? "adult" : "minor"
puts labelTernary Inside Strings
The ternary shines inside string interpolation, where a full if/else would not fit cleanly.
count = 1
puts "You have #{count} item#{count == 1 ? "" : "s"}"When to Use Which
Quick guide:
if/elsif/elsefor general branching.unlessfor a single negative check that reads naturally.- The ternary for choosing between two simple values.
Readability always wins over cleverness.
n = 7
parity = n.even? ? "even" : "odd"
puts "#{n} is #{parity}"Quick Check
Test your understanding of branching.
Recap
You learned Ruby's branching tools:
if/elsif/elserun code based on truthiness.ifis an expression that returns a value.- Modifier forms (
do_x if cond) suit short one-liners. unlessisif !— use it for clean negative checks.- The ternary
? :picks between two values concisely.
grade = 88
result = grade >= 60 ? "pass" : "fail"
puts "Result: #{result}"Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “if, unless, ternary” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “if, unless, ternary”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
What will I learn in “if, unless, ternary”?
Branching basics. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “if, unless, ternary” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if, unless, ternary
- case/when Expressions
- Truthiness in Ruby
- Guard Clauses