case/when Conditionals

A case expression is Ruby's multi-branch conditional: it tests a subject against each when clause using the === operator and runs the first branch that matches.

Learn case/when Conditionals in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Because === is defined by ranges, classes, and regexes, one case can switch on numeric ranges, types, and patterns — and it returns a value you can assign.

What You'll Learn in This Lesson

1️⃣ Basic case/when & Returning a Value

Write case subject , then one when per option. Ruby checks each in order and runs the first match — no break needed, because there's no fall-through. List several values with commas, and remember the whole expression returns a value you can assign.

2️⃣ Matching Classes & Regexes

Since every class defines === as an instance check, you can branch on type with when Integer , when String , and so on. A regex's === tests for a match, so when /\\d+/ matches any string containing a digit.

3️⃣ case Without a Subject

Leave the subject off after case and each when becomes a full boolean test — an elegant stand-in for a long if/elsif chain when your conditions don't share a single value.

Your turn. Fill in each ___ blank, then run it.

Combine exact values, comma-separated values, and ranges in one case . Run with ruby status.rb .

📋 Quick Reference — case/when

Practice quiz

Which operator does case/when use to test each branch?

  • ==
  • <=>
  • ===
  • eql?

Answer: ===. case/when matches using the case-equality operator === (when_value === subject).

For when 80..89 matching subject 85, which comparison does Ruby actually evaluate?

  • (80..89) === 85
  • 85 === (80..89)
  • 85 == 80..89
  • (80..89) <=> 85

Answer: (80..89) === 85. The when value is on the LEFT of ===, so Ruby tests (80..89) === 85, which is true.

Given grade = 85, what does this print? case grade; when 90..100 then puts "A"; when 80..89 then puts "B"; else puts "F"; end

  • A
  • F
  • Nothing
  • B

Answer: B. 85 falls in the 80..89 range, so the second branch runs and prints B.

Why does when Integer match the value 42?

  • Because 42.is_a?(when) is true
  • Because Integer === 42 returns true (Class#=== is an instance check)
  • Because 42 == Integer
  • It does not match

Answer: Because Integer === 42 returns true (Class#=== is an instance check). Class defines === as an instance check, so Integer === 42 is true and the branch matches.

Does Ruby's case/when fall through to later branches like C's switch?

  • No — only the first matching branch runs, no break needed
  • Yes, you need break to stop it
  • Only for string subjects
  • Only inside a loop

Answer: No — only the first matching branch runs, no break needed. There is no fall-through in Ruby; only the first matching when runs, so break is unnecessary.

How do you make several values share one branch?

  • when "Sat" or "Sun"

List multiple values with commas: when "Sat", "Sun" then ... matches either.

What does a case with NO subject behave like?

  • An infinite loop
  • An if/elsif chain where each when is a full boolean test
  • A method definition
  • It is a syntax error

Answer: An if/elsif chain where each when is a full boolean test. Subject-less case turns each when into a standalone boolean condition, like if/elsif.

A case expression itself:

  • Returns nil always
  • Cannot be assigned to a variable
  • Returns the subject
  • Returns the value of the branch that ran, so you can assign it

Answer: Returns the value of the branch that ran, so you can assign it. case is an expression returning the matched branch's value, so label = case x ... end works.

What does when /\d+/ match against a string?

  • Strings that are exactly digits
  • Strings that contain at least one digit
  • Only the number zero
  • Empty strings

Answer: Strings that contain at least one digit. Regexp#=== tests for a match, so /\d+/ matches any string containing one or more digits.

Why does when Numeric match both an Integer and a Float?

  • Numeric is a special keyword
  • Ruby converts everything to Numeric
  • Class matching with === respects inheritance, and both subclass Numeric
  • It only matches Integer, not Float

Answer: Class matching with === respects inheritance, and both subclass Numeric. Integer and Float both descend from Numeric, and === honors inheritance, so both match.