SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
All Objects Are Created .equal?
  Understand equality in your Ruby codez
github.com/gsterndale/equality
Equality methods


a == b

a === b

a.eql? b

a.equal? b
==
Everyday equality (==)



a == b
Default ==

a = MyBasicClass.new
b = MyBasicClass.new

a == b
# => false

b = a

a == b
# => true
Overriding ==
class RomanNumeral

  def ==(other)
    if other.respond_to?(:to_f)
      self.to_f == other.to_f
    else
      false
    end
  end

end
Overriding ==


iv   = RomanNumeral.new('IV')

iiii = RomanNumeral.new('IIII')

iv == iiii
# => true
===
case statement equality (===)



a === b
Default ===

a = Object.new
b = Object.new

case   a
when   b
  'b   must === a'
else
  'b   must NOT === a'
end
# =>   "b must NOT === a"
Float ===

a = 1
b = 1.0

case   a
when   b
  'b   must === a'
else
  'b   must NOT === a'
end
# =>   "b must === a"
Regexp ===


case '123'
when /d+/
  'At least one number'
else
  'No numbers found'
end
# => "At least one number"
When === != ==


/d+/ == '123'
# => false

/d+/ === '123'
# => true
Class ===


case 'abc'
when String
  'It is a String!'
else
  'Not a String'
end
# => "It is a String!"
Asymmetry


/d+/ === '123'
# => true

'123' === /d+/
# => false
Asymmetry

2 === Integer
# => false

Integer === 2
# => true

Fixnum === 2
# => true
.equal?
Object equality (.equal?)

a = 'FOO'
b = a

a.equal? b
# => true

a.equal? 'FOO'
# => false
.eql?
Hash key equality (.eql?)



a.eql? b
Default .eql?
foo = Object.new
hash = { foo => 'My value' }

bar = Object.new

foo.equal? bar
# => false
foo.eql? bar
# => false

hash[bar]
# => nil
String .eql?
foo = 'My Key'
hash = { foo => 'My value' }

bar = 'My Key'

foo.equal? bar
# => false
foo.eql? bar
# => true

hash[bar]
# => "My value"
Overriding .eql?

class RomanNumeral

  def eql?(other)
    other.kind_of?(RomanNumeral) &&
      self.to_i.eql?(other.to_i)
  end

end
Overriding .eql?
iv   = RomanNumeral.new('IV')

hash = { iv => 'Four' }

iiii = RomanNumeral.new('IIII')

iv.equal? iiii
# => false
iv.eql? iiii
# => true

hash[iiii]
# => "Four"
Comparable
Comparison methods
# You must define <=>
a <=> b
# => -1, 0, 1 -or- nil

a == b

a   > b
a   < b
a   >= b
a   <= b

c.between?(a, b)
Overriding <=>
class RomanNumeral

  def <=>(other)
    if other.respond_to?(:to_f)
      self.to_f <=> other.to_f
    else
      nil
    end
  end

end
Overriding <=>
v = RomanNumeral.new('V')
x = RomanNumeral.new('X')

v <=> x
# => -1

v >= x
# => false

RomanNumeral.new('VIII').between?(v, x)
# => true
Sorting Enumerables

iv   = RomanNumeral.new('IV')
iiii = RomanNumeral.new('IIII')
x    = RomanNumeral.new('X')

[iv, x, iiii]
# => [IV, X, IIII]

[iv, x, iiii].sort
# => [IV, IIII, X]

Más contenido relacionado

Destacado

plaY [commercial]
plaY [commercial]plaY [commercial]
plaY [commercial]smwarfield
 
Personagraph Whitepaper
Personagraph WhitepaperPersonagraph Whitepaper
Personagraph WhitepaperTapan Kamdar
 
Digital Trends Impacting News Companies
Digital Trends Impacting News CompaniesDigital Trends Impacting News Companies
Digital Trends Impacting News CompaniesReid Williams
 
Server Side 2009
Server Side 2009Server Side 2009
Server Side 2009vaclav.lohr
 
Copyright and Fair Use for USU Extension
Copyright and Fair Use for USU ExtensionCopyright and Fair Use for USU Extension
Copyright and Fair Use for USU ExtensionBritt Fagerheim
 
Ctm louvre
Ctm louvreCtm louvre
Ctm louvreclaireso
 
Change history with Git
Change history with GitChange history with Git
Change history with Gitgsterndale
 
Como subir una actividad o tarea a moodle
Como subir una actividad o tarea a moodleComo subir una actividad o tarea a moodle
Como subir una actividad o tarea a moodleJose Ramirez
 
SEO pro manažery
SEO pro manažerySEO pro manažery
SEO pro manažeryvaclav.lohr
 
Smartfren Network Test Drive Jakarta - Yogyakarta
Smartfren Network Test Drive Jakarta - YogyakartaSmartfren Network Test Drive Jakarta - Yogyakarta
Smartfren Network Test Drive Jakarta - YogyakartaJarwadi MJ
 
Lunch Menus and Recipes from Portugal
Lunch Menus and Recipes from PortugalLunch Menus and Recipes from Portugal
Lunch Menus and Recipes from PortugalTiina Sarisalmi
 
Integrating Library Resources into Blackboard
Integrating Library Resources into BlackboardIntegrating Library Resources into Blackboard
Integrating Library Resources into BlackboardBritt Fagerheim
 
Christmas Handicraft by Thanasis
Christmas Handicraft by ThanasisChristmas Handicraft by Thanasis
Christmas Handicraft by ThanasisTiina Sarisalmi
 
Library As Teaching Resource
Library As Teaching ResourceLibrary As Teaching Resource
Library As Teaching ResourceBritt Fagerheim
 

Destacado (20)

What WELD does
What WELD doesWhat WELD does
What WELD does
 
plaY [commercial]
plaY [commercial]plaY [commercial]
plaY [commercial]
 
Personagraph Whitepaper
Personagraph WhitepaperPersonagraph Whitepaper
Personagraph Whitepaper
 
Digital Trends Impacting News Companies
Digital Trends Impacting News CompaniesDigital Trends Impacting News Companies
Digital Trends Impacting News Companies
 
Server Side 2009
Server Side 2009Server Side 2009
Server Side 2009
 
Copyright and Fair Use for USU Extension
Copyright and Fair Use for USU ExtensionCopyright and Fair Use for USU Extension
Copyright and Fair Use for USU Extension
 
Third comeback report 4.8,2011
Third comeback report 4.8,2011Third comeback report 4.8,2011
Third comeback report 4.8,2011
 
Ctm louvre
Ctm louvreCtm louvre
Ctm louvre
 
The vmware story
The vmware storyThe vmware story
The vmware story
 
Change history with Git
Change history with GitChange history with Git
Change history with Git
 
Czech Day in Kozani
Czech Day in KozaniCzech Day in Kozani
Czech Day in Kozani
 
Como subir una actividad o tarea a moodle
Como subir una actividad o tarea a moodleComo subir una actividad o tarea a moodle
Como subir una actividad o tarea a moodle
 
Sinsai.info and Crisis Mapping
Sinsai.info and Crisis MappingSinsai.info and Crisis Mapping
Sinsai.info and Crisis Mapping
 
SEO pro manažery
SEO pro manažerySEO pro manažery
SEO pro manažery
 
Smartfren Network Test Drive Jakarta - Yogyakarta
Smartfren Network Test Drive Jakarta - YogyakartaSmartfren Network Test Drive Jakarta - Yogyakarta
Smartfren Network Test Drive Jakarta - Yogyakarta
 
Lunch Menus and Recipes from Portugal
Lunch Menus and Recipes from PortugalLunch Menus and Recipes from Portugal
Lunch Menus and Recipes from Portugal
 
Integrating Library Resources into Blackboard
Integrating Library Resources into BlackboardIntegrating Library Resources into Blackboard
Integrating Library Resources into Blackboard
 
Christmas Handicraft by Thanasis
Christmas Handicraft by ThanasisChristmas Handicraft by Thanasis
Christmas Handicraft by Thanasis
 
Kort Om Etikk2
Kort Om Etikk2Kort Om Etikk2
Kort Om Etikk2
 
Library As Teaching Resource
Library As Teaching ResourceLibrary As Teaching Resource
Library As Teaching Resource
 

Último

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 Scriptwesley chun
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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, Adobeapidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
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 DiscoveryTrustArc
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Último (20)

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
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+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...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

All Objects are created .equal?