If I want to use a case..when to switch based on the kind of an object instance, I can simply do:
case obj
when Class1
"whatever"
when Class2
"something else"
end
But if the value I’m comparing is a class itself, and I want to switch based on the class it descends from, does case..when still hold up? The following doesn’t work, but does anything?
class Foo < String; end
class Bar < Array; end
klass = Foo
case klass
when String
"klass descends from String"
when Array
"klass descends from Array"
end
Basically what the <= operator does, but in the context of a case..when.
EDIT | Just to clarify, I want to support matching anywhere in the inheritance chain, not just the immediate parent.
This works:
I’m not sure if it’s any better than a bunch of if and elsif’s though.