Ruby Control Structures - Case (Bonus Question)

  • + 0 comments

    From the Ruby documentation:

    Case equality (success by a when candidate) is determined by the case-equality or “threequal” operator, ===.

    So you know this already, because you are specifically mentioning

    There is something I can't figure out about the === operator when applied to Classes...

    But did you know that:

    === is typically overriden by classes to reflect meaningful case-statement behavior; for example, /abc/ === "string" checks for a pattern match from the string.

    In fact, if you actually go and look at the Ruby documentation on the Class class, you'll notice that it doesn't have a definition of ===. This behavior is delegated to Object

    So when you execute the following:

    case obj.class
    when Hacker
    	puts "It's a Hacker!"
    ...
    

    You're comparing the Class of HackerRank (which is a class) to the Class Hacker via the === operator.

    You can verify this by calling obj.class.class because the curried .class is what is being compared with the case statement