case/in Basics
Structural matching.
case/in Basics 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.
Pattern Matching Arrives
Ruby's case/in expression performs structural pattern matching: it tests the shape and content of data, not just equality. It is a powerful upgrade over case/when.
case 42
in Integer
puts 'it is an integer'
endcase/in vs case/when
case/when compares with === (equality-style). case/in deconstructs values and can bind variables. Use in for matching structure.
case 'hello'
in String => s
puts "string of length #{s.length}"
endMatching Literals
An in branch can match a literal value, working like a precise equality check.
case 3
in 1 then puts 'one'
in 3 then puts 'three'
in _ then puts 'other'
endType Patterns
Naming a class matches any value of that type, leveraging === under the hood.
def describe(x)
case x
in Integer then 'number'
in String then 'text'
in Array then 'list'
end
end
puts(describe([1, 2]))Binding Variables
A bare identifier in a pattern binds the matched value to a new variable you can use in the branch.
case [1, 2]
in [a, b]
puts(a + b)
endThe Wildcard
The underscore _ matches anything without binding. Use it as a catch-all or to ignore positions.
case 99
in 0 then puts 'zero'
in _ then puts 'something else'
endPin Operator
To match against the value of an existing variable (instead of binding a new one), pin it with ^.
expected = 5
case 5
in ^expected
puts 'matched the pinned value'
endAlternative Patterns
Combine patterns with | to match any of several alternatives in one branch.
case 'b'
in 'a' | 'b' | 'c'
puts 'in the first three letters'
endBinding the Whole Match
Use pattern => name to test a pattern and bind the matched value to a name simultaneously.
case 'Ruby'
in String => word
puts(word.upcase)
endNoMatchingPatternError
If no in branch matches and there is no else, Ruby raises NoMatchingPatternError. Add an else to handle the rest safely.
case 10
in 1 then puts 'one'
else
puts 'no specific match'
endOne-Line Matching
The => operator does a one-line rightward assignment match, raising if it fails. The in operator returns true/false instead.
result = [1, 2] in [Integer, Integer]
puts resultQuick Check
What happens if a case/in has no matching branch and no else?
Recap: case/in Basics
You learned structural matching fundamentals:
case/inmatches shape, not just equality- Literals, type patterns, and the
_wildcard - Variable binding and the pin operator
^ - Alternatives with
|andpattern => name - Missing match raises
NoMatchingPatternError
case 7
in Integer => n then puts(n * 2)
endLearn 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 “case/in Basics” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “case/in Basics”, 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 “case/in Basics”?
Structural matching. 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 “case/in Basics” 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.