0
0
Rubyprogramming~20 mins

Case with ranges and patterns in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AGood
BExcellent
CPass
DFail
Attempts:
2 left
💡 Hint
Check which range 85 falls into.
Predict Output
intermediate
2: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
ANo match
BRuntimeError
CSyntaxError
DMatched pattern
Attempts:
2 left
💡 Hint
Check if the array matches the pattern with two integers followed by 3.
Predict Output
advanced
2: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"
end
AY is y_val
BY is 20
CNo match
DSyntaxError
Attempts:
2 left
💡 Hint
Look at how y is bound in the pattern.
Predict Output
advanced
2: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
AHigh
BLow
CMedium
DSyntaxError
Attempts:
2 left
💡 Hint
Check which range 7 belongs to.
Predict Output
expert
3: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"
end
AActive user Alice, age 30
BNo match
CSyntaxError
DRuntimeError
Attempts:
2 left
💡 Hint
Look carefully at the nested hash pattern and variable binding.