case and when
A case statement names a value once and tests it against each when clause with ===, running only the first match, so ranges, classes and lists all work as patterns.
Follow case matching rules instead of assuming every comparison is simple equality.
Bean Cart's receipt printer needs a label for every order size: 1 cup, 2 to 5 cups, 6 or more, or something invalid. A tower of elsif lines comparing the same variable again and again is tiring to read. case states the variable once and lists the possibilities underneath.
The rule
casenames the value once; eachwhentests it with===.
label =
case cups
when 1 then "single"
when 2..5 then "small batch"
when (6..) then "bulk"
else "invalid"
endHow it works: Ruby takes the value after case and checks each when from top to bottom. The first one that matches runs, and the rest are skipped. There is no fall-through into the next branch, so no break is needed. else handles everything that matched nothing, and without an else the whole case returns nil. Like if, case is an expression that returns a value.
The matching uses ===, the case-equality operator from the comparison lesson, called on the when value: when 2..5 really asks (2..5) === cups. What === does depends on what you put after when:
After when |
Matches when | Example |
|---|---|---|
| A plain value | equal | when "tea" |
| A Range | the value falls inside | when 2..5 |
| A class | the value is of that class | when Integer |
| A regular expression | the text matches the pattern | when /latte/ |
One when can list several options separated by commas, meaning "any of these":
case drink
when "latte", "mocha", "flat white" then "milk drink"
when "espresso" then "black"
else "ask the barista"
endYou can also write case with no value at all. Then each when holds a full condition, and it behaves like a tidy if/elsif chain.
Ruby 3 adds a second form, case ... in, for pattern matching on the shape of arrays and hashes. It is powerful and worth learning later; case ... when is the everyday tool.