elsif and case : case « Statement « Ruby






elsif and case


fruit = "orange"
if fruit == "orange"
  color = "orange"
elsif fruit == "apple"
  color = "green"
elsif fruit == "banana"
  color = "yellow"
else
  color = "unknown"
end


fruit = "orange"
case fruit
  when "orange"
    color = "orange"
  when "apple"
    color = "green"
  when "banana"
    color = "yellow"
  else
    color = "unknown"
end

 








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.All Ruby expressions return a result
8.Case with regular expression