Reputation: 9896
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?
Upvotes: 415
Views: 206414
Reputation: 17189
In a case
statement, a ,
is the equivalent of ||
in an if
statement.
case car
when 'toyota', 'lexus'
puts 'brand is part of the Toyota Motor Corporation'
else
puts 'some other brand'
end
# NOTE: You may use `then` after the when condition.
# It is frequently used to place the body of the `when` on a single line.
case car
when 'toyota', 'lexus' then puts 'brand is part of the Toyota Motor Corporation'
else puts 'some other brand'
end
Upvotes: 896
Reputation: 1
In a case statement, equivalent of && in an if statement.
case coding_language when 'ror' && 'javascript' # code end
Upvotes: -3
Reputation: 39
Remember switch/case (case/when, etc.) is just comparing values. I like the official answer in this instance for a simple or'd string list comparison, but for more exotic conditional / matching logic,
case true
when ['honda', 'acura'].include?(car)
# do something
when (condition1 && (condition2 || condition3))
# do something different
else
# do something else
end
Upvotes: 3
Reputation: 179
you could do something like this (inspired by @pilcrow's answer):
honda = %w[honda acura civic element fit ...]
toyota = %w[toyota lexus tercel rx yaris ...]
honda += %w[ev_ster concept_c concept_s ...] if include_concept_cars
case car
when *toyota
# Do something for Toyota cars
when *honda
# Do something for Honda cars
...
end
Upvotes: 0
Reputation: 58741
You might take advantage of ruby's "splat" or flattening syntax.
This makes overgrown when
clauses — you have about 10 values to test per branch if I understand correctly — a little more readable in my opinion. Additionally, you can modify the values to test at runtime. For example:
honda = ['honda', 'acura', 'civic', 'element', 'fit', ...]
toyota = ['toyota', 'lexus', 'tercel', 'rx', 'yaris', ...]
...
if include_concept_cars
honda += ['ev-ster', 'concept c', 'concept s', ...]
...
end
case car
when *toyota
# Do something for Toyota cars
when *honda
# Do something for Honda cars
...
end
Another common approach would be to use a hash as a dispatch table, with keys for each value of car
and values that are some callable object encapsulating the code you wish to execute.
Upvotes: 123
Reputation: 1509
Another nice way to put your logic in data is something like this:
# Initialization.
CAR_TYPES = {
foo_type: ['honda', 'acura', 'mercedes'],
bar_type: ['toyota', 'lexus']
# More...
}
@type_for_name = {}
CAR_TYPES.each { |type, names| names.each { |name| @type_for_name[type] = name } }
case @type_for_name[car]
when :foo_type
# do foo things
when :bar_type
# do bar things
end
Upvotes: 0