Pattern matching
case-in
Pattern matching tests the shape of a value with case and in instead of a chain of conditions.
When the hash has the expected keys, the code after in runs with the extracted parts.
value = { status: "ok", code: 200 } case value in { status: "ok", code: code } puts "OK: #{code}" else puts "Other" end
ruby case_demo.rb
OK: 200
Bind variables
Patterns can bind parts of a value to new variables for the branch body.
First takes the head of the array while rest collects the remaining elements.
case [1, 2, 3] in [first, *rest] puts first p rest end
ruby bind_demo.rb
1
[2, 3]
Match arrays
Array patterns match element by element, mixing exact values with captures and wildcards.
200 with a message prints the message, while a bare star swallows any tail.
case [200, "OK"] in [200, message] puts message in [404, *] puts "Missing" else puts "Other" end
ruby array_demo.rb
OK
Match hashes
Hash patterns match by key and can check value types while binding results.
The String check guarantees a real name, and the admin literal restricts the role.
user = { name: "Ada", role: "admin" } case user in { name: String => name, role: "admin" } puts "Admin #{name}" else puts "Guest" end
ruby hash_demo.rb
Admin Ada
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
value = { status: "ok", code: 200 } case value in { status: "ok", code: code } puts "OK: #{code}" else puts "Other" endAnswer
OK: 200
The hash has both keys, the second is bound to code, and the branch prints it.
-
What does this program print?
case [1, 2, 3] in [first, *rest] puts first p rest endAnswer
1 / [2, 3]
The head is bound to first and the star collects the remaining elements into an array.
-
What does this program print?
user = { name: 42, role: "admin" } case user in { name: String => name, role: "admin" } puts "Admin #{name}" else puts "Guest" endAnswer
Guest
The type check fails, so the branch does not match and the else branch runs.
-
What does this program print?
case [404, "Not Found", "extra"] in [404, *] puts "Missing" else puts "Other" endAnswer
Missing
A bare star matches any tail, so the array arm matches and the branch prints its message.
-
What does a hash pattern require of the value?
Answer
The value is a hash that holds at least the keys of the pattern
A pattern names the keys it cares about, and extra keys do not stop the match.
-
What does
in [200, message]bind?Answer
The second element, when the first is the literal 200
The literal must match, and the lower-case name captures the element in that position.
-
When does the
elsebranch of acase ... inrun?Answer
When no pattern matched
The else is the fallback for a value whose shape matches none of the patterns.
-
What does
{ name: String => name }check?Answer
That the value under name is a String, and binds it to name
The class before the arrow is a type check, and the name after it is the binding.
-
Which pattern matches only a two-element array?
Answer
[first, second]
Two names and no star describe exactly two elements.
-
What is the advantage of
case-inover a chain ofifconditions?Answer
It tests the shape of the value and binds its parts in one expression
The pattern is the test and the destructuring at once, which is what keeps the branches short.
Article author: Arthur Isaev