So see the below code:
s1 = "a"
=> "a"
s1.class
=> String
s1.class == String
=> true
s1.class === String
=> false
String == String
=> true
String === String
=> false
String === s1
=> true
String == s1
=> false
s1 == String
=> false
s1 === String
=> false
my question is ->
- why
String == String
evaluates to true butString === String
does not?
Is it because in fact those are different objects and are stored in different parts of memory? If yes then why would we initialize many Class objects of String? (shouldn't those be kind of a singleton?)
String inherits from Object and has Comparable module included.
From Object String gets the .===
(https://ruby-doc.org/core-2.5.1/Object.html#method-i-3D-3D-3D)
and from Comparable it gets the .==
(https://ruby-doc.org/core-2.4.0/Comparable.html#method-i-3D-3D)
From reading the definitions I see that the .===
is typically the same as .==
but that's not the case with String. I don't know why though.
- Why
s1 === String
is false butString === s1
is true?
I assume it's because the .===
implementation on "a" object of a String is not the same that .===
implementation on the String class, but how does the .===
(and maybe why does it work in this way) on String work (how does it know it should compare the class of the object and not the value/place in memory)?