Case statement with multiple values in each 'when' block

Nick picture Nick · Apr 17, 2012 · Viewed 130.7k times · Source

The best way I can describe what I'm looking for is to show you the failed code I've tried thus far:

case car
  when ['honda', 'acura'].include?(car)
    # code
  when 'toyota' || 'lexus'
    # code
end

I've got about 4 or 5 different when situations that should be triggered by approximately 50 different possible values of car. Is there a way to do this with case blocks or should I try a massive if block?

Answer

Charles Caldwell picture Charles Caldwell · Apr 17, 2012

In a case statement, a , is the equivalent of || in an if statement.

case car
   when 'toyota', 'lexus'
      # code
end

Some other things you can do with a Ruby case statement