Challenge - 5 Problems
Ruby Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of case with range and pattern matching
What is the output of this Ruby code?
Ruby
score = 85 result = case score when 90..100 then "Excellent" when 75..89 then "Good" when 50..74 then "Pass" else "Fail" end puts result
Attempts:
2 left
💡 Hint
Check which range 85 falls into.
✗ Incorrect
The variable score is 85, which falls into the range 75..89, so the case returns "Good".
❓ Predict Output
intermediate2:00remaining
Pattern matching with case and array
What will this Ruby code print?
Ruby
value = [1, 2, 3] case value in [Integer, Integer, 3] puts "Matched pattern" else puts "No match" end
Attempts:
2 left
💡 Hint
Check if the array matches the pattern with two integers followed by 3.
✗ Incorrect
The array [1, 2, 3] matches the pattern [Integer, Integer, 3], so it prints "Matched pattern".
❓ Predict Output
advanced2:00remaining
Case with pattern matching and variable binding
What is the output of this Ruby code?
Ruby
point = { x: 10, y: 20 }
case point
in { x: 10, y: y_val }
puts "Y is #{y_val}"
else
puts "No match"
endAttempts:
2 left
💡 Hint
Look at how y is bound in the pattern.
✗ Incorrect
The pattern matches a hash with x:10 and binds y to y_val, which is 20, so it prints "Y is 20".
❓ Predict Output
advanced2:00remaining
Case with range and pattern matching combined
What will this Ruby code output?
Ruby
value = 7 case value in 1..5 puts "Low" in 6..10 puts "Medium" else puts "High" end
Attempts:
2 left
💡 Hint
Check which range 7 belongs to.
✗ Incorrect
7 is in the range 6..10, so the case prints "Medium".
❓ Predict Output
expert3:00remaining
Complex pattern matching with nested structures
What is the output of this Ruby code?
Ruby
data = { user: { name: "Alice", age: 30 }, status: :active }
case data
in { user: { name: "Alice", age: age_val }, status: :active }
puts "Active user Alice, age #{age_val}"
else
puts "No match"
endAttempts:
2 left
💡 Hint
Look carefully at the nested hash pattern and variable binding.
✗ Incorrect
The pattern matches the nested hash and binds age to age_val (30), so it prints "Active user Alice, age 30".