Ruby has a fairly powerful case..when..else
construct for when you need to match criteria against a single variable. What is the "canonical" way to match criteria against multiple variables without simply nesting case
statements?
Wrapping multiple variables in an array (like [x, y]
) and matching against it isn't equivalent, because Ruby won't apply the magical case ===
operator to the elements of the array; the operator is only applied to the array itself.
I'm going to go ahead and respond with a community-wiki answer with a (defeated) stab at this question.
You need to use an if..elsif..else
, and ensure that the variables you want to match against appear on the right-hand side of the ===
operator (which is what case
essentially does).
For example, if you want to match x
and y
against some criteria:
if (SomeType === x) && (1..10 === y)
some_value
elsif (:some_symbol === x) && (11..20 === y)
some_other_value
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With