As we have seen in the previous lessons, we can check for multiple conditions using the if/elsif/else expression.
A more simplified and flexible option is the case expression, which tests a value in when statements.
a = 2
# outputs “Two”
Multiple values can be tested within a single when by separating the values with commas.
An else statement can be provided to execute code if no when condition matches:
age = 18
when 1, 2, 3
puts “Little baby”
when 4, 5
puts “Not a baby”
# outputs “Not a baby”
© License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT