0
0
Rubyprogramming~10 mins

Case with ranges and patterns in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Case with ranges and patterns
Start with a value
Enter case statement
Check each pattern or range
Match found?
YesExecute matching branch
Exit case
No match
Check next pattern
No matches
Execute else branch if present
End
The case statement checks the value against each pattern or range in order, executes the first matching branch, or else runs the else branch if no match.
Execution Sample
Ruby
score = 75
case score
when 0..59
  grade = 'F'
when 60..69
  grade = 'D'
when 70..79
  grade = 'C'
else
  grade = 'A'
end
puts grade
This code assigns a grade based on the score using ranges in a case statement.
Execution Table
StepValuePattern CheckedMatch?ActionOutput
1750..59NoCheck next pattern
27560..69NoCheck next pattern
37570..79YesAssign grade = 'C'
475elseSkippedExit case
575N/AN/APrint gradeC
💡 Match found at 70..79 range, grade assigned 'C', case exits.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
score7575757575
gradenilnilnil'C''C'
Key Moments - 3 Insights
Why does the case statement stop checking after the 70..79 range?
Because the value 75 matches the 70..79 range at step 3, the case executes that branch and exits immediately, skipping the else branch (see execution_table row 4).
What happens if the value does not match any range and there is no else branch?
The case statement would do nothing and no variable would be assigned. Since there is no else, no branch runs (not shown in this example).
Can patterns in a case statement be ranges and other types?
Yes, Ruby case statements can match ranges, exact values, classes, or use pattern matching. Here we use ranges for numeric intervals.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'grade' after step 2?
A'D'
B'F'
Cnil
D'C'
💡 Hint
Check variable_tracker column 'After Step 2' for 'grade' value.
At which step does the case statement find a matching pattern?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table 'Match?' column to find first 'Yes'.
If the score was 50, which pattern would match according to the execution table logic?
A0..59
B60..69
C70..79
Delse
💡 Hint
Ranges are checked in order; 50 fits in 0..59 range.
Concept Snapshot
Ruby case statement with ranges:
case value
when range1
  # code
when range2
  # code
else
  # code
end
Checks each pattern in order, executes first match, else runs else branch.
Full Transcript
This example shows how Ruby's case statement can use ranges to match a value. The program starts with a score of 75. It checks if 75 is in 0..59 (no), then 60..69 (no), then 70..79 (yes). When it finds a match, it assigns grade 'C' and exits the case. Finally, it prints the grade. If no match was found, the else branch would run. Variables 'score' and 'grade' change as shown in the variable tracker. This helps beginners see how case with ranges works step-by-step.