Bird
0
0

How can you use a case/when statement to check multiple values in one when clause?

hard📝 Application Q9 of 15
Ruby - Control Flow

How can you use a case/when statement to check multiple values in one when clause?

status = 'pending'
Acase status when 'pending' && 'waiting' && 'queued' puts 'In progress' else puts 'Other' end
Bcase status when ['pending', 'waiting', 'queued'] puts 'In progress' else puts 'Other' end
Ccase status when 'pending' || 'waiting' || 'queued' puts 'In progress' else puts 'Other' end
Dcase status when 'pending', 'waiting', 'queued' puts 'In progress' else puts 'Other' end
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple values in when

    Ruby allows comma-separated values in a single when clause to match any of them.
  2. Step 2: Analyze each option

    case status when 'pending', 'waiting', 'queued' puts 'In progress' else puts 'Other' end uses commas correctly. case status when ['pending', 'waiting', 'queued'] puts 'In progress' else puts 'Other' end uses an array which does not match directly. Options C and D use logical operators incorrectly.
  3. Final Answer:

    case status when 'pending', 'waiting', 'queued' puts 'In progress' else puts 'Other' end -> Option D
  4. Quick Check:

    Use commas to list multiple when values [OK]
Quick Trick: Separate multiple when values with commas [OK]
Common Mistakes:
MISTAKES
  • Using arrays instead of commas
  • Using logical operators inside when
  • Expecting || or && to work in when

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes