Find Patterns and Guards
Conditions in matches.
Find Patterns and Guards is a free Ruby Academy lesson on CoddyKit — lesson 3 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.
Guard Conditions
Add an if or unless guard to a pattern. The branch matches only when both the pattern and the condition succeed.
case 7
in Integer => n if n.odd?
puts 'odd integer'
endGuards Use Bound Variables
The guard can reference variables bound by the pattern, so you can express rich conditions about the matched data.
case [4, 9]
in [a, b] if a < b
puts 'ascending pair'
endunless Guards
An unless guard matches when the condition is false, the inverse of if.
case 0
in Integer => n unless n.positive?
puts 'zero or negative'
endMultiple Guarded Branches
Combine guards across branches to build a clear decision ladder.
def grade(score)
case score
in Integer => s if s >= 90 then 'A'
in Integer => s if s >= 70 then 'B'
else 'C'
end
end
puts(grade(85))Find Patterns
A find pattern [*, x, *] searches an array for a sub-pattern anywhere inside it, with splats on both ends.
case [1, 2, 3, 4]
in [*, 3, *]
puts 'contains a 3'
endBinding Inside a Find
The matched element in a find pattern can be bound, capturing the found value.
case [10, 20, 30]
in [*, x, *] if x > 25
puts("found #{x}")
endCapturing Surroundings
Both splats in a find pattern can be named to capture the elements before and after the match.
case [1, 2, 99, 4, 5]
in [*before, 99, *after]
p before
p after
endFind a Typed Element
Find patterns combine with type patterns to locate the first element of a given type.
case [1, 'hi', 2]
in [*, String => word, *]
puts(word)
endGuards vs Patterns
Express what you can structurally in the pattern, and use guards only for conditions patterns cannot capture (like numeric comparisons or method calls).
case { role: 'admin', age: 40 }
in { role: 'admin', age: } if age >= 18
puts 'adult admin'
endCombining Find and Guard
Find patterns and guards together let you ask: does any element satisfy this condition?
numbers = [3, 5, 8, 11]
case numbers
in [*, n, *] if n.even?
puts("first even-ish hit: #{n}")
endOrder of Branches
Branches are tried top to bottom; the first match wins. Put more specific guarded patterns before general ones.
case 100
in Integer => n if n > 50 then puts 'big'
in Integer then puts 'small'
endQuick Check
What does the find pattern [*, x, *] do?
Recap: Find Patterns and Guards
You added conditions and search to matching:
- Guards
if/unlessrefine a pattern - Guards can use bound variables
- Find patterns
[*, x, *]search anywhere - Both splats can be named to capture surroundings
- First matching branch wins, so order matters
case [1, 7, 3]
in [*, x, *] if x > 5 then puts x
endFrequently asked questions
Is the “Find Patterns and Guards” lesson free?
Yes — the full text of “Find Patterns and Guards” 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 “Find Patterns and Guards”?
Conditions in matches. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Find Patterns and Guards” 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
- case/in Basics
- Array and Hash Patterns
- Find Patterns and Guards
- Practical Use Cases