case Expressions
case matches a value against when clauses, including ranges, classes and regexes.
Example
def grade(s)
case s
when 90.. then "A"
when 70...90 then "B"
else "C"
end
end
puts grade(95), grade(75), grade(10)
puts (case 5 when Integer then "int" else "other" end)Result
A B C int
Exercise
Match clause.
case x 1 then "a" end
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.