Case with regular expression : case « Statement « Ruby






Case with regular expression


string = "123"

case string
    when /^[a-zA-Z]+$/
      "Letters"
    when /^[0-9]+$/
      "Numbers"
    else
      "Mixed"
end
# => "Numbers"

 








Related examples in the same category

1.Here's the general format of the Ruby case statement:
2.Using the case Statement
3.If vs Case
4.case...when with range
5.use exculsive ranges in case statements
6.use a case statement to make an assignment, which omits the value to test
7.elsif and case
8.All Ruby expressions return a result