That's because it's more like a compacted if/else ladder than what I'd traditionally think of a switch as (indexed jump).
SQL has CASE...WHEN which is a similar concept.
SQL has CASE...WHEN which is a similar concept.
if (patternA.match(input))
elsif (patternB.match(input))
else
end
vs case input
when patternA
when patternB
else
end
And since it uses ===, you can define that on your own classes too. Hypothetically for instance, you can make fairly complex policy type classes to make reusable boolean statements. case current_user
when NotValidatedEmail
when NotCompletedOnboarding
when SomethingElse
else
end
and then you can use those elsewhere in the code by leveraging the more global use of === [user1, user2].any?(NotCompletedOnboarding)It's more of an implementation detail, but the Ruby VM actually does a jump if all the cases are "keyable"(typically integers, strings, symbols, etc). It does a hash lookup and jump to the returning address.
https://github.com/ruby/ruby/blob/92466e440d459cd21e89f8bfbe...
I should go read some of the interpreter code at some point - fun work going on there.