Find Patterns and Guards
Conditions in matches.
Find Patterns and Guards is a free Ruby Academy lesson on CoddyKit. This is lesson 3 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.
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
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 “Find Patterns and Guards” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Find Patterns and Guards”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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