Is there any special Reason why the === and the == methods are implemented differently in the class Class?
"".class == String
#=> true
"".class === String
#=> false
Is there any special Reason why the === and the == methods are implemented differently in the class Class?
"".class == String
#=> true
"".class === String
#=> false
The goal of === (called "case quality") is to be used within case statement. Creators decided that aliasing === with is_a? for Module instances works best with a common scenarios like:
def do_sth(object)
case object
when :nothing then ...
when String then ...
when Hash then ...
else ...
end
=== should (almost) never be explicitly used outside of the case statement.
We can further simplify your question I think. I believe you are asking is why
String == String # true
But
String === String # false
I think it's semi consistent by Ruby. the === equality asks if right side is a member of the left side.
So
Class === String
Is true since String is a member of Class. And indeed String is not a member of String.
What I do find weird though is that
5 === 5 # returns true
Imo it should return false to be consistent with String === String returning false, but for primitives Ruby has this quirk, probably so it works well with case statements.