Find Patterns and Guards
Conditions in matches.
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'
endAll lessons in this course
- case/in Basics
- Array and Hash Patterns
- Find Patterns and Guards
- Practical Use Cases