How do you write a switch statement in Ruby?

In Ruby, a C-style switch statement is written with the case expression. Unlike a C-style switch, there is no "fall through". Ruby case expressions use the triple equals operator for comparison, meaning that you can compare against things like ranges.


case foo
when 3..7
  puts "foo is a number between 3 and 7"
when "bar"
  puts "foo is 'bar'!"
when 1992, 1993
  puts "foo is a year when the Blue Jays won the world series!"
  puts "foo is #{foo.inspect}"