SlideShare uma empresa Scribd logo
1 de 9
Equality in
Ruby
Romain Sempé
@rsempe
Equality or Identity?


—  Equality:
       Different objects that represent the same value.


—  Identity:
       Different variables that refer to the same object.
Equal yes! But in what sense?

—  What equality methods should we use?
    —  ==
    —  eql?
    —  equal?
    —  ===
    —  =~
==
Test only if the objects have the same value.
eql?
Test if the objects have the same value and the
                    same type.
equal?
Test if the objects have the same value, the same
  type and are the same instance in memory.
===
    Same as == but for case statements.
One operator depending on the context of the
  comparison (Class, Range, Regexp, …).
=~
Interesting with the Regexp class.
It’s an alias to the match method.
Conclusion

—  == is great most of the time

—  eql? and equal? can be useful to be more precise

—  Elegant case statements with ===

—  Concise test for Regexp with =~

—  Only one identity operator: equal?

Mais conteúdo relacionado

Destaque

เวกเตอร์และสเกลาร์ พื้นฐาน
เวกเตอร์และสเกลาร์ พื้นฐานเวกเตอร์และสเกลาร์ พื้นฐาน
เวกเตอร์และสเกลาร์ พื้นฐาน
Gl Start
 
Informe del impacto de audi en instagram
Informe del impacto de audi en instagramInforme del impacto de audi en instagram
Informe del impacto de audi en instagram
alerties
 
Bata home online shopping procedure
Bata home online shopping procedureBata home online shopping procedure
Bata home online shopping procedure
Frankie Mwendah
 
Introduction to Networked Dynamical Systems with focus on Distributed Coordin...
Introduction to Networked Dynamical Systems with focus on Distributed Coordin...Introduction to Networked Dynamical Systems with focus on Distributed Coordin...
Introduction to Networked Dynamical Systems with focus on Distributed Coordin...
Marzieh Nabi
 
Karangan pertahanan awam
Karangan pertahanan awamKarangan pertahanan awam
Karangan pertahanan awam
Adlin Salsain
 

Destaque (14)

UX Design for Long-Term Results
UX Design for Long-Term ResultsUX Design for Long-Term Results
UX Design for Long-Term Results
 
เวกเตอร์และสเกลาร์ พื้นฐาน
เวกเตอร์และสเกลาร์ พื้นฐานเวกเตอร์และสเกลาร์ พื้นฐาน
เวกเตอร์และสเกลาร์ พื้นฐาน
 
Informe del impacto de audi en instagram
Informe del impacto de audi en instagramInforme del impacto de audi en instagram
Informe del impacto de audi en instagram
 
Bata home online shopping procedure
Bata home online shopping procedureBata home online shopping procedure
Bata home online shopping procedure
 
Ch 2 ppt
Ch 2 pptCh 2 ppt
Ch 2 ppt
 
Ch 10 ppt
Ch 10 pptCh 10 ppt
Ch 10 ppt
 
Introduction to Networked Dynamical Systems with focus on Distributed Coordin...
Introduction to Networked Dynamical Systems with focus on Distributed Coordin...Introduction to Networked Dynamical Systems with focus on Distributed Coordin...
Introduction to Networked Dynamical Systems with focus on Distributed Coordin...
 
Ch 5 ppt
Ch 5 pptCh 5 ppt
Ch 5 ppt
 
Karangan pertahanan awam
Karangan pertahanan awamKarangan pertahanan awam
Karangan pertahanan awam
 
Ch 7 ppt
Ch 7 pptCh 7 ppt
Ch 7 ppt
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Mobile Functional Beauty - Trebbble
Mobile Functional Beauty - TrebbbleMobile Functional Beauty - Trebbble
Mobile Functional Beauty - Trebbble
 
Ch 6 ppt
Ch 6 pptCh 6 ppt
Ch 6 ppt
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Equality in Ruby

  • 2. Equality or Identity? —  Equality: Different objects that represent the same value. —  Identity: Different variables that refer to the same object.
  • 3. Equal yes! But in what sense? —  What equality methods should we use? —  == —  eql? —  equal? —  === —  =~
  • 4. == Test only if the objects have the same value.
  • 5. eql? Test if the objects have the same value and the same type.
  • 6. equal? Test if the objects have the same value, the same type and are the same instance in memory.
  • 7. === Same as == but for case statements. One operator depending on the context of the comparison (Class, Range, Regexp, …).
  • 8. =~ Interesting with the Regexp class. It’s an alias to the match method.
  • 9. Conclusion —  == is great most of the time —  eql? and equal? can be useful to be more precise —  Elegant case statements with === —  Concise test for Regexp with =~ —  Only one identity operator: equal?