SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
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]

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Kürzlich hochgeladen

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 

Kürzlich hochgeladen (20)

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 

All Objects are created .equal?