Why Ruby cases do not work with classes?

case "Hello".class when Integer print "A" when String print "B" else print "C" end 

Why am I getting a "C"? Expected "B", because if you evaluate to "String".class , you get String .

+4
source share
1 answer

Confusingly, the Ruby case uses === to compare each case with an object. Class#=== tests for instances of this class, but not the class itself:

 > Fixnum === Integer false > Fixnum === 1 true 

The case behavior that Ruby is trying to advertise:

 case "Hello" when Integer puts "A" when String puts "B" else puts "C" end 
+6
source

All Articles