Recall & Review
beginner
What is a
case statement used for in Ruby?A
case statement lets you check a value against multiple conditions and run code based on which condition matches. It's like choosing between many options.Click to reveal answer
beginner
How do ranges work inside a Ruby
case statement?Ranges like
1..5 represent a sequence of values. In a case, Ruby checks if the value falls inside the range to decide which code to run.Click to reveal answer
intermediate
What is pattern matching in Ruby's
case statement?Pattern matching lets you check if a value fits a certain shape or structure, like matching parts of an array or hash, and then run code based on that.
Click to reveal answer
beginner
Explain this Ruby code snippet:<br>
case age when 0..12 then "Child" when 13..19 then "Teen" else "Adult" end
This code checks the value of
age. If it's between 0 and 12, it returns "Child". If between 13 and 19, it returns "Teen". Otherwise, it returns "Adult".Click to reveal answer
intermediate
How does Ruby decide which
when clause matches in a case statement?Ruby compares the
case value with each when condition using the === operator. The first match runs its code, and then Ruby stops checking further.Click to reveal answer
In Ruby, what does
1..5 represent inside a case statement?✗ Incorrect
The
1..5 syntax creates a range including numbers 1, 2, 3, 4, and 5.What will this Ruby code output if
score = 85?<br>case score when 90..100 then "Excellent" when 75..89 then "Good" else "Needs Improvement" end
✗ Incorrect
85 falls in the range 75..89, so the output is "Good".
Which operator does Ruby use internally to check
when conditions in a case statement?✗ Incorrect
Ruby uses the
=== operator to test if the case value matches a when condition.What does pattern matching in a Ruby
case statement allow you to do?✗ Incorrect
Pattern matching lets you check if a value fits a certain structure, like parts of an array or hash.
What will this Ruby code return?<br>
case [1, 2, 3] when [1, 2, 3] then "Exact match" when [1, 2] then "Partial match" else "No match" end
✗ Incorrect
The array matches exactly the first
when condition, so it returns "Exact match".Explain how Ruby's
case statement works with ranges and how it decides which block to execute.Think about how Ruby checks if a value falls inside a range.
You got /4 concepts.
Describe what pattern matching means in Ruby's
case statement and give an example of when it might be useful.Consider matching parts of a data structure, not just simple values.
You got /4 concepts.