0Pricing
Ruby Academy · Lesson

case/when Expressions

Multi-way branching.

case/when Expressions is a free Ruby Academy lesson on CoddyKit — lesson 2 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.

Multi-way Branching

When you compare one value against many possibilities, a long if/elsif chain gets noisy. Ruby's case expression makes multi-way branching clean and readable.

day = "Mon"
case day
when "Sat", "Sun"
  puts "Weekend"
else
  puts "Weekday"
end

Basic case / when

The value after case is compared to each when in order. The first match runs, then control jumps to end.

grade = "B"
case grade
when "A"
  puts "Excellent"
when "B"
  puts "Good"
when "C"
  puts "Okay"
end

Multiple Values per when

List several values in one when separated by commas. If any of them match, that branch runs.

char = "e"
case char
when "a", "e", "i", "o", "u"
  puts "Vowel"
else
  puts "Consonant"
end

The else Branch

Add else as a catch-all. Without it, an unmatched case simply returns nil.

code = 500
case code
when 200
  puts "OK"
when 404
  puts "Not Found"
else
  puts "Other status"
end

case Is an Expression

Like if, case returns the value of the matched branch, so you can assign it directly.

score = 82
letter = case
when score >= 90 then "A"
when score >= 80 then "B"
else "C"
end
puts letter

then Keyword

Use then to put the body on the same line as when for short branches.

n = 3
case n
when 1 then puts "one"
when 2 then puts "two"
when 3 then puts "three"
end

Matching Ranges

A when can hold a Range. Ruby uses the === operator behind the scenes, and a Range's === checks membership.

score = 73
case score
when 90..100 then puts "A"
when 70..89 then puts "B"
when 0..69 then puts "C"
end

Matching by Class

Because Class === object is true when the object is an instance, you can branch on type.

value = 3.14
case value
when Integer then puts "an integer"
when Float then puts "a float"
when String then puts "a string"
end

The Magic of ===

Every when calls pattern === value. Ranges, Classes, and Regexps all define useful === behavior, which is why case is so flexible.

email = "jane@example.com"
case email
when /@/ then puts "Looks like an email"
else puts "No @ sign"
end

case with No Subject

Omit the value after case to write a clean chain of boolean conditions, like a tidy if/elsif.

temp = 5
case
when temp < 0 then puts "Freezing"
when temp < 15 then puts "Cold"
when temp < 25 then puts "Mild"
else puts "Warm"
end

Putting It Together

case is ideal when one value drives the decision. Reach for the subjectless form when each branch is a separate boolean test.

role = :admin
msg = case role
when :admin then "Full access"
when :editor then "Can edit"
when :viewer then "Read only"
else "No access"
end
puts msg

Quick Check

Test your understanding of case/when.

Recap

You learned the case expression:

  • when matches the first hit, then jumps to end.
  • One when can list several values.
  • case returns a value and supports then.
  • Ranges, Classes, and Regexps work because when uses ===.
  • The subjectless form is a clean boolean chain.
n = 42
kind = case
when n.zero? then "zero"
when n.positive? then "positive"
else "negative"
end
puts kind

Frequently asked questions

Is the “case/when Expressions” lesson free?

Yes — the full text of “case/when Expressions” 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 “case/when Expressions”?

Multi-way branching. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “case/when Expressions” 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