The case/when statement helps you choose what to do based on different conditions. It makes your code cleaner and easier to read when you have many choices.
Case/when statement in Ruby
case variable when value1 # code to run if variable === value1 when value2 # code to run if variable === value2 else # code to run if no match end
case keyword starts the statement, and when checks each possible value.else part is optional and runs if no when matches.day and prints a message based on it.case day when "Monday" puts "Start of the week" when "Friday" puts "Almost weekend" else puts "Just another day" end
when line.case number when 1, 2, 3 puts "Small number" when 4..6 puts "Medium number" else puts "Other number" end
This program asks the user to enter a fruit name and then tells them which fruit they chose using a case/when statement.
puts "Enter a fruit name:" fruit = gets.chomp case fruit.downcase when "apple" puts "You chose apple." when "banana" puts "You chose banana." when "orange" puts "You chose orange." else puts "Unknown fruit." end
The case statement compares using === operator, so it works well with ranges and classes.
Remember to use end to close the case statement.
The case/when statement helps you pick actions based on a value.
It is cleaner than many if/else statements when checking many options.
You can check multiple values or ranges in one when line.
Practice
What does the case/when statement do in Ruby?
Solution
Step 1: Understand the purpose of case/when
The case/when statement compares a value to different options to decide which code to run.Step 2: Compare with other options
Repeating code is done by loops, defining methods usesdef, and variables are created by assignment, so these are incorrect.Final Answer:
It chooses which code to run based on matching a value. -> Option AQuick Check:
case/when selects code by value match [OK]
- Confusing case/when with loops
- Thinking case/when defines methods
- Believing case/when creates variables
Which of the following is the correct syntax for a case/when statement in Ruby?
case x
when 1
puts "One"
when 2
puts "Two"
else
puts "Other"
endSolution
Step 1: Check Ruby case/when syntax
Ruby usescasefollowed bywhenclauses and ends withend. No braces or colons are used.Step 2: Compare options
case x when 1 puts "One" when 2 puts "Two" else puts "Other" end matches Ruby syntax exactly. Options B and C use braces and colons like other languages. if x == 1 then puts "One" elsif x == 2 then puts "Two" else puts "Other" end is an if/elsif, not case/when.Final Answer:
case x when 1 puts "One" when 2 puts "Two" else puts "Other" end -> Option AQuick Check:
Ruby case/when uses no braces or colons [OK]
- Using braces or colons like other languages
- Confusing case/when with switch/case from other languages
- Using if/elsif instead of case/when
What is the output of this Ruby code?
grade = 'B' case grade when 'A' puts "Excellent" when 'B', 'C' puts "Good" when 'D' puts "Pass" else puts "Fail" end
Solution
Step 1: Identify the value of grade
The variablegradeis set to 'B'.Step 2: Match grade in case/when
Thewhen 'B', 'C'line matches 'B', so it runsputs "Good".Final Answer:
Good -> Option DQuick Check:
grade 'B' matches when 'B', 'C' [OK]
- Only checking first value in when list
- Confusing else output
- Ignoring multiple values in one when
Find the error in this Ruby code snippet:
score = 85 case score when 90..100 puts "A" when 80..89 puts "B" when 70..79 puts "C" else puts "F" end
Why does it not print "B" for score 85?
Solution
Step 1: Understand how case/when works with ranges
Ruby's case/when uses the===operator, which works with ranges to check if a value is inside.Step 2: Check the given score and ranges
Score 85 falls inside 80..89, soputs "B"will run correctly.Final Answer:
The code is correct; it will print "B" as expected. -> Option CQuick Check:
Ranges work with case/when using === [OK]
- Thinking ranges must be strings
- Believing case/when can't handle ranges
- Assuming overlapping ranges cause errors here
You want to write a Ruby case/when statement that prints "Weekend" for Saturday and Sunday, "Weekday" for Monday to Friday, and "Invalid" for any other input. Which code correctly does this?
Solution
Step 1: Understand how to list multiple values in when
Use commas to separate multiple values in one when line, likewhen 'Saturday', 'Sunday'.Step 2: Check each option
case day when 'Saturday', 'Sunday' puts "Weekend" when 'Monday'..'Friday' puts "Weekday" else puts "Invalid" end incorrectly uses a range of strings 'Monday'..'Friday' which does not work as expected for days. case day when ['Saturday', 'Sunday'] puts "Weekend" when ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] puts "Weekday" else puts "Invalid" end uses arrays in when, which does not match values correctly. case day when 'Saturday'..'Sunday' puts "Weekend" when 'Monday'..'Friday' puts "Weekday" else puts "Invalid" end uses string ranges like 'Saturday'..'Sunday' which is not meaningful for days. case day when 'Saturday', 'Sunday' puts "Weekend" when 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' puts "Weekday" else puts "Invalid" end correctly lists each weekday separately with commas.Final Answer:
case day when 'Saturday', 'Sunday' puts "Weekend" when 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' puts "Weekday" else puts "Invalid" end -> Option BQuick Check:
Use commas for multiple values in when [OK]
- Using ranges of strings for days
- Using arrays inside when clauses
- Trying to use string ranges that don't work
Ready to prove it?
Pass the test and this pattern moves unknown
