ruby class and object - different methods of comparison [duplicate]=== vs. == in RubyWhat's the difference between equal?, eql?, ===, and ==?Why are exclamation marks used in Ruby methods?A concise explanation of nil v. empty v. blank in Ruby on RailsHow to write a switch statement in RubyWhat does Ruby have that Python doesn't, and vice versa?Check if a value exists in an array in Rubyclass << self idiom in RubyWhat is attr_accessor in Ruby?Ruby craziness: Class vs Object?What's the difference between equal?, eql?, ===, and ==?How to check if an object has a singleton class (eigenclass) without creating one
Is `x >> pure y` equivalent to `liftM (const y) x`
Would a high gravity rocky planet be guaranteed to have an atmosphere?
How can a function with a hole (removable discontinuity) equal a function with no hole?
Did Dumbledore lie to Harry about how long he had James Potter's invisibility cloak when he was examining it? If so, why?
Implement the Thanos sorting algorithm
Inappropriate reference requests from Journal reviewers
Gears on left are inverse to gears on right?
Lay out the Carpet
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
What is the intuitive meaning of having a linear relationship between the logs of two variables?
India just shot down a satellite from the ground. At what altitude range is the resulting debris field?
How to pronounce the slash sign
How did Doctor Strange see the winning outcome in Avengers: Infinity War?
Is expanding the research of a group into machine learning as a PhD student risky?
Opposite of a diet
Unreliable Magic - Is it worth it?
What happens if you roll doubles 3 times then land on "Go to jail?"
What is paid subscription needed for in Mortal Kombat 11?
Is HostGator storing my password in plaintext?
Do the temporary hit points from the Battlerager barbarian's Reckless Abandon stack if I make multiple attacks on my turn?
Why not increase contact surface when reentering the atmosphere?
Large drywall patch supports
Class Action - which options I have?
Purchasing a ticket for someone else in another country?
ruby class and object - different methods of comparison [duplicate]
=== vs. == in RubyWhat's the difference between equal?, eql?, ===, and ==?Why are exclamation marks used in Ruby methods?A concise explanation of nil v. empty v. blank in Ruby on RailsHow to write a switch statement in RubyWhat does Ruby have that Python doesn't, and vice versa?Check if a value exists in an array in Rubyclass << self idiom in RubyWhat is attr_accessor in Ruby?Ruby craziness: Class vs Object?What's the difference between equal?, eql?, ===, and ==?How to check if an object has a singleton class (eigenclass) without creating one
This question already has an answer here:
=== vs. == in Ruby
3 answers
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 == Stringevaluates 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 === Stringis false butString === s1is 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)?
ruby
marked as duplicate by Kris, lurker, Community♦ Mar 8 at 12:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
=== vs. == in Ruby
3 answers
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 == Stringevaluates 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 === Stringis false butString === s1is 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)?
ruby
marked as duplicate by Kris, lurker, Community♦ Mar 8 at 12:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The same thing happens incase,when Stringwill not match, I assumecaseuses===.
– Kris
Mar 8 at 11:34
The difference between those two operator is clearly explained here.
– Tashi Dendup
Mar 8 at 11:44
I agree, this question is explained in the duplicate pointed by @Kris
– beniutek
Mar 8 at 12:39
add a comment |
This question already has an answer here:
=== vs. == in Ruby
3 answers
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 == Stringevaluates 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 === Stringis false butString === s1is 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)?
ruby
This question already has an answer here:
=== vs. == in Ruby
3 answers
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 == Stringevaluates 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 === Stringis false butString === s1is 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)?
This question already has an answer here:
=== vs. == in Ruby
3 answers
ruby
ruby
asked Mar 8 at 11:20
beniutekbeniutek
441518
441518
marked as duplicate by Kris, lurker, Community♦ Mar 8 at 12:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Kris, lurker, Community♦ Mar 8 at 12:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The same thing happens incase,when Stringwill not match, I assumecaseuses===.
– Kris
Mar 8 at 11:34
The difference between those two operator is clearly explained here.
– Tashi Dendup
Mar 8 at 11:44
I agree, this question is explained in the duplicate pointed by @Kris
– beniutek
Mar 8 at 12:39
add a comment |
The same thing happens incase,when Stringwill not match, I assumecaseuses===.
– Kris
Mar 8 at 11:34
The difference between those two operator is clearly explained here.
– Tashi Dendup
Mar 8 at 11:44
I agree, this question is explained in the duplicate pointed by @Kris
– beniutek
Mar 8 at 12:39
The same thing happens in
case, when String will not match, I assume case uses ===.– Kris
Mar 8 at 11:34
The same thing happens in
case, when String will not match, I assume case uses ===.– Kris
Mar 8 at 11:34
The difference between those two operator is clearly explained here.
– Tashi Dendup
Mar 8 at 11:44
The difference between those two operator is clearly explained here.
– Tashi Dendup
Mar 8 at 11:44
I agree, this question is explained in the duplicate pointed by @Kris
– beniutek
Mar 8 at 12:39
I agree, this question is explained in the duplicate pointed by @Kris
– beniutek
Mar 8 at 12:39
add a comment |
1 Answer
1
active
oldest
votes
You are right == and === are different methods on the String class and an instance of String. Have a look at the different documentation for
Module.==Module.===String#==String#===
Why String == String evaluates to true but String === String does not?
As you can see in the docs Module.== basically means if both sides are the same Object. Is the String class the same as the String class? Yes. But Module.=== returns true if the right side is an instance of the class on the left. Id String an instance of String? No.
Why s1 === String is false but String === s1 is true?
s1 === String calls === on an instance of string. This method returns true when both sides are the same object. Are an instance of String and the class Sting the same object? No. But String === s1 has – as already explained before the meaning of is_a?: Is an instance of String an instance of String? Yes.
.===returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!
– beniutek
Mar 8 at 12:36
It may be worth pointing out that===is also called the Case Equality as stated in theObject#===documentation. This operator is called when you provide it aswhenargument.case 'Hello World!'; when /ello/ then true; end #=> trueorcase 15; when 1..20 then true; end #=> true
– 3limin4t0r
Mar 8 at 12:52
Enumerable#grepalso uses this operator.[5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25]or[5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']
– 3limin4t0r
Mar 8 at 12:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are right == and === are different methods on the String class and an instance of String. Have a look at the different documentation for
Module.==Module.===String#==String#===
Why String == String evaluates to true but String === String does not?
As you can see in the docs Module.== basically means if both sides are the same Object. Is the String class the same as the String class? Yes. But Module.=== returns true if the right side is an instance of the class on the left. Id String an instance of String? No.
Why s1 === String is false but String === s1 is true?
s1 === String calls === on an instance of string. This method returns true when both sides are the same object. Are an instance of String and the class Sting the same object? No. But String === s1 has – as already explained before the meaning of is_a?: Is an instance of String an instance of String? Yes.
.===returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!
– beniutek
Mar 8 at 12:36
It may be worth pointing out that===is also called the Case Equality as stated in theObject#===documentation. This operator is called when you provide it aswhenargument.case 'Hello World!'; when /ello/ then true; end #=> trueorcase 15; when 1..20 then true; end #=> true
– 3limin4t0r
Mar 8 at 12:52
Enumerable#grepalso uses this operator.[5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25]or[5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']
– 3limin4t0r
Mar 8 at 12:57
add a comment |
You are right == and === are different methods on the String class and an instance of String. Have a look at the different documentation for
Module.==Module.===String#==String#===
Why String == String evaluates to true but String === String does not?
As you can see in the docs Module.== basically means if both sides are the same Object. Is the String class the same as the String class? Yes. But Module.=== returns true if the right side is an instance of the class on the left. Id String an instance of String? No.
Why s1 === String is false but String === s1 is true?
s1 === String calls === on an instance of string. This method returns true when both sides are the same object. Are an instance of String and the class Sting the same object? No. But String === s1 has – as already explained before the meaning of is_a?: Is an instance of String an instance of String? Yes.
.===returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!
– beniutek
Mar 8 at 12:36
It may be worth pointing out that===is also called the Case Equality as stated in theObject#===documentation. This operator is called when you provide it aswhenargument.case 'Hello World!'; when /ello/ then true; end #=> trueorcase 15; when 1..20 then true; end #=> true
– 3limin4t0r
Mar 8 at 12:52
Enumerable#grepalso uses this operator.[5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25]or[5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']
– 3limin4t0r
Mar 8 at 12:57
add a comment |
You are right == and === are different methods on the String class and an instance of String. Have a look at the different documentation for
Module.==Module.===String#==String#===
Why String == String evaluates to true but String === String does not?
As you can see in the docs Module.== basically means if both sides are the same Object. Is the String class the same as the String class? Yes. But Module.=== returns true if the right side is an instance of the class on the left. Id String an instance of String? No.
Why s1 === String is false but String === s1 is true?
s1 === String calls === on an instance of string. This method returns true when both sides are the same object. Are an instance of String and the class Sting the same object? No. But String === s1 has – as already explained before the meaning of is_a?: Is an instance of String an instance of String? Yes.
You are right == and === are different methods on the String class and an instance of String. Have a look at the different documentation for
Module.==Module.===String#==String#===
Why String == String evaluates to true but String === String does not?
As you can see in the docs Module.== basically means if both sides are the same Object. Is the String class the same as the String class? Yes. But Module.=== returns true if the right side is an instance of the class on the left. Id String an instance of String? No.
Why s1 === String is false but String === s1 is true?
s1 === String calls === on an instance of string. This method returns true when both sides are the same object. Are an instance of String and the class Sting the same object? No. But String === s1 has – as already explained before the meaning of is_a?: Is an instance of String an instance of String? Yes.
answered Mar 8 at 11:49
spickermannspickermann
61.2k75880
61.2k75880
.===returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!
– beniutek
Mar 8 at 12:36
It may be worth pointing out that===is also called the Case Equality as stated in theObject#===documentation. This operator is called when you provide it aswhenargument.case 'Hello World!'; when /ello/ then true; end #=> trueorcase 15; when 1..20 then true; end #=> true
– 3limin4t0r
Mar 8 at 12:52
Enumerable#grepalso uses this operator.[5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25]or[5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']
– 3limin4t0r
Mar 8 at 12:57
add a comment |
.===returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!
– beniutek
Mar 8 at 12:36
It may be worth pointing out that===is also called the Case Equality as stated in theObject#===documentation. This operator is called when you provide it aswhenargument.case 'Hello World!'; when /ello/ then true; end #=> trueorcase 15; when 1..20 then true; end #=> true
– 3limin4t0r
Mar 8 at 12:52
Enumerable#grepalso uses this operator.[5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25]or[5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']
– 3limin4t0r
Mar 8 at 12:57
.=== returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!– beniutek
Mar 8 at 12:36
.=== returns true is if the right side is the INSTANCE of that class. I've missed that somehow in the docs. thanks!– beniutek
Mar 8 at 12:36
It may be worth pointing out that
=== is also called the Case Equality as stated in the Object#=== documentation. This operator is called when you provide it as when argument. case 'Hello World!'; when /ello/ then true; end #=> true or case 15; when 1..20 then true; end #=> true– 3limin4t0r
Mar 8 at 12:52
It may be worth pointing out that
=== is also called the Case Equality as stated in the Object#=== documentation. This operator is called when you provide it as when argument. case 'Hello World!'; when /ello/ then true; end #=> true or case 15; when 1..20 then true; end #=> true– 3limin4t0r
Mar 8 at 12:52
Enumerable#grep also uses this operator. [5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25] or [5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']– 3limin4t0r
Mar 8 at 12:57
Enumerable#grep also uses this operator. [5, 25, 83, nil, 'hey'].grep(5..30) #=> [5, 25] or [5, 25, 83, nil, 'hey'].grep(String) #=> ['hey']– 3limin4t0r
Mar 8 at 12:57
add a comment |
The same thing happens in
case,when Stringwill not match, I assumecaseuses===.– Kris
Mar 8 at 11:34
The difference between those two operator is clearly explained here.
– Tashi Dendup
Mar 8 at 11:44
I agree, this question is explained in the duplicate pointed by @Kris
– beniutek
Mar 8 at 12:39