SlideShare a Scribd company logo
1 of 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?

More Related Content

Viewers also liked

UX Design for Long-Term Results
UX Design for Long-Term ResultsUX Design for Long-Term Results
UX Design for Long-Term ResultsTrebbble
 
เวกเตอร์และสเกลาร์ พื้นฐาน
เวกเตอร์และสเกลาร์ พื้นฐานเวกเตอร์และสเกลาร์ พื้นฐาน
เวกเตอร์และสเกลาร์ พื้นฐาน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 instagramalerties
 
Bata home online shopping procedure
Bata home online shopping procedureBata home online shopping procedure
Bata home online shopping procedureFrankie 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 awamAdlin Salsain
 
Mobile Functional Beauty - Trebbble
Mobile Functional Beauty - TrebbbleMobile Functional Beauty - Trebbble
Mobile Functional Beauty - TrebbbleTrebbble
 

Viewers also liked (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
 

Recently uploaded

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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 DevelopmentsTrustArc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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)wesley chun
 
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?Igalia
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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 productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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...Martijn de Jong
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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)
 
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?
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Ruby Equality and Identity Operators Explained

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