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













0
















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 ->



  1. why String == String evaluates to true but String === 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.



  1. Why s1 === String is false but String === 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)?










share|improve this 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 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











  • I agree, this question is explained in the duplicate pointed by @Kris

    – beniutek
    Mar 8 at 12:39















0
















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 ->



  1. why String == String evaluates to true but String === 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.



  1. Why s1 === String is false but String === 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)?










share|improve this 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 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











  • I agree, this question is explained in the duplicate pointed by @Kris

    – beniutek
    Mar 8 at 12:39













0












0








0









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 ->



  1. why String == String evaluates to true but String === 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.



  1. Why s1 === String is false but String === 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)?










share|improve this question















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 ->



  1. why String == String evaluates to true but String === 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.



  1. Why s1 === String is false but String === 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)?





This question already has an answer here:



  • === vs. == in Ruby

    3 answers







ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • 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 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • .=== 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











  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer























  • .=== 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











  • 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
















1














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.






share|improve this answer























  • .=== 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











  • 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














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • .=== 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











  • 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

















.=== 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






Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme