0Pricing
Ruby Academy · Lesson

if, unless, ternary

Branching basics.

if, unless, ternary is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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"
end

if / 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"
end

elsif Chains

Use elsif to test multiple conditions in order. Ruby checks them top to bottom and runs the first match.

  • Note the spelling: elsif, not elseif or else if.
score = 75
if score >= 90
  puts "A"
elsif score >= 70
  puts "B"
elsif score >= 50
  puts "C"
else
  puts "F"
end

if 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 status

Modifier 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 > 30

Meet 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"
end

Modifier unless

Like if, unless also works as a trailing modifier for one-liners.

stock = 0
puts "Out of stock" unless stock > 0

Avoid 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/else

The 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 label

Ternary 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 / else for general branching.
  • unless for 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 / else run code based on truthiness.
  • if is an expression that returns a value.
  • Modifier forms (do_x if cond) suit short one-liners.
  • unless is if ! — use it for clean negative checks.
  • The ternary ? : picks between two values concisely.
grade = 88
result = grade >= 60 ? "pass" : "fail"
puts "Result: #{result}"

Frequently asked questions

Is the “if, unless, ternary” lesson free?

Yes — the full text of “if, unless, ternary” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. if, unless, ternary
  2. case/when Expressions
  3. Truthiness in Ruby
  4. Guard Clauses
← Back to Ruby Academy