Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ruby. How is the case equality method implemented in the class Class?

Is there any special Reason why the === and the == methods are implemented differently in the class Class?

>> "".class == String
=> true
>> "".class === String
=> false

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading