Recall & Review
beginner
What is a
case/when statement in Ruby?It is a control structure that lets you compare a value against multiple conditions and execute code based on the first matching condition.
Click to reveal answer
beginner
How do you write a simple
case/when statement in Ruby?Use
case followed by the value, then multiple when clauses for conditions, and else for default. Example:<br>case x when 1 puts "One" when 2 puts "Two" else puts "Other" end
Click to reveal answer
intermediate
Can a
when clause check multiple values at once?Yes! You can separate values with commas to check if the case value matches any of them. Example:<br>
case x when 1, 3, 5 puts "Odd number" end
Click to reveal answer
intermediate
What happens if no
when condition matches and there is no else clause?The
case statement returns nil and no code inside the when blocks runs.Click to reveal answer
intermediate
How is
case/when different from multiple if/elsif statements?Both control flow choices check conditions, but
case/when is cleaner when comparing one value against many options, making code easier to read.Click to reveal answer
What keyword starts a case statement in Ruby?
✗ Incorrect
The
case keyword starts the case statement.How do you specify multiple values in a single
when clause?✗ Incorrect
Separate values with commas to check if the case value matches any of them.
What does the
else clause do in a case statement?✗ Incorrect
The
else clause runs if no when condition matches.What value does a case statement return if no conditions match and no
else is given?✗ Incorrect
It returns
nil and does not run any code inside when blocks.Which is a benefit of using
case/when over multiple if/elsif?✗ Incorrect
case/when is cleaner and easier to read when comparing one value to many options.Explain how a
case/when statement works in Ruby and give a simple example.Think about how you check one value against many choices.
You got /4 concepts.
Describe how you can check multiple values in a single
when clause and why that might be useful.Imagine you want to run the same code for several possible values.
You got /3 concepts.