Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case statement with multiple values in each 'when' block

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?

like image 918
Nick Avatar asked Apr 17 '12 18:04

Nick


People also ask

Can a single CASE statement contain multiple values?

[Solved] A single Case statement can contain multiple values.

Can we have multiple statements in a single case block?

You can use multiple break statements inside a single case block in JavaScript and it will act just like multiple return statements inside of a single function.

Can a case when statement return multiple values?

@yzhang - With CASE only the first match will return values. If you want the possibility of multiple conditions mathcing each input row, you need to make each check indpendantly, and UNION the results together. Both @Yuck and I have answers that will fullfil that for you.

Can you have multiple conditions in a CASE statement?

Multiple conditions in CASE statementYou can evaluate multiple conditions in the CASE statement.


1 Answers

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

like image 104
Charles Caldwell Avatar answered Sep 22 '22 20:09

Charles Caldwell