Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Case with ranges and patterns
Start with a value
↓
Enter case statement
↓
Check each pattern or range
↓
Match found?
Yes→Execute 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.

Practice

(1/5)
1.

What does the in keyword do inside a case statement in Ruby when used with ranges?

easy
A. It checks if the value falls within the given range.
B. It assigns the range to the value.
C. It creates a new range object.
D. It compares the value for equality with the range.

Solution

  1. Step 1: Understand the role of in in case

    The in keyword is used to match a value against a pattern or range inside a case statement.
  2. Step 2: Check how it works with ranges

    When used with a range like 1..10, it checks if the value lies within that range.
  3. Final Answer:

    It checks if the value falls within the given range. -> Option A
  4. Quick Check:

    in with range means value in range [OK]
Hint: Remember: in checks membership in range or pattern [OK]
Common Mistakes:
  • Thinking in assigns values instead of matching
  • Confusing range equality with membership
  • Assuming in creates new objects
2.

Which of the following is the correct syntax to match a number n in the range 5 to 15 inside a case statement using pattern matching?

case n
  in ???
    puts "In range"
  else
    puts "Out of range"
end
easy
A. in (5..15)
B. 5..15
C. 5...15
D. in 5..15

Solution

  1. Step 1: Identify correct pattern syntax in in clause

    Inside case, in is followed directly by the pattern, so in 5..15 is correct syntax.
  2. Step 2: Check other options for syntax errors

    Options without in or with extra parentheses are invalid syntax in this context.
  3. Final Answer:

    in 5..15 -> Option D
  4. Quick Check:

    Use in followed by the pattern without extra keywords or parentheses [OK]
Hint: Use in once, then the pattern without extra keywords [OK]
Common Mistakes:
  • Writing in in 5..15 which is invalid
  • Adding parentheses around the range
  • Using three-dot range 5...15 which excludes 15
3.

What will be the output of this Ruby code?

score = 75
case score
in 0..49 then puts "Fail"
in 50..79 then puts "Pass"
in 80..100 then puts "Excellent"
else puts "Invalid"
end
medium
A. Pass
B. Fail
C. Excellent
D. Invalid

Solution

  1. Step 1: Check which range includes 75

    75 is not in 0..49 but is in 50..79.
  2. Step 2: Match the corresponding output

    The code prints "Pass" when score is in 50..79.
  3. Final Answer:

    Pass -> Option A
  4. Quick Check:

    75 in 50..79 means output "Pass" [OK]
Hint: Find which range includes the value, then pick that output [OK]
Common Mistakes:
  • Choosing "Fail" because 75 is greater than 49
  • Confusing inclusive and exclusive ranges
  • Ignoring the order of case branches
4.

Identify the error in this Ruby code snippet using case with pattern matching:

value = 10
case value
in 1..5
  puts "Low"
in 6..10
  puts "Medium"
else
  puts "High"
end
medium
A. Missing then after in clauses
B. No error; code runs and prints "Medium"
C. Ranges should be written as arrays inside in
D. The case statement should use when instead of in

Solution

  1. Step 1: Check syntax of case with in and ranges

    Using in with ranges inside case is valid Ruby syntax.
  2. Step 2: Verify output for value 10

    10 matches the range 6..10, so it prints "Medium".
  3. Final Answer:

    No error; code runs and prints "Medium" -> Option B
  4. Quick Check:

    in with ranges works fine [OK]
Hint: Remember in works like when for pattern matching [OK]
Common Mistakes:
  • Thinking then is mandatory after in
  • Confusing in with when
  • Trying to write ranges as arrays
5.

Given this Ruby code, what will be the output?

data = [3, 7, 12, 18]
case data
in [Integer, Integer, Integer, Integer]
  puts "All integers"
in [Integer, Integer, Integer, 18]
  puts "Last is 18"
else
  puts "No match"
end
hard
A. No match
B. Last is 18
C. All integers
D. SyntaxError

Solution

  1. Step 1: Understand array pattern matching order

    The case checks patterns in order. The first pattern matches any array of four integers.
  2. Step 2: Check if data matches first pattern

    Data is [3,7,12,18], all integers, so it matches the first pattern.
  3. Step 3: Confirm second pattern is not reached

    Since first pattern matches, second is skipped.
  4. Final Answer:

    All integers -> Option C
  5. Quick Check:

    First matching pattern runs, so output is "All integers" [OK]
Hint: Patterns checked top-down; first match wins [OK]
Common Mistakes:
  • Assuming second pattern runs despite first matching
  • Confusing element types with values in patterns
  • Expecting syntax error due to pattern complexity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard