Your SlideShare is downloading. ×
0
All Objects Are Created .equal?  Understand equality in your Ruby codez
github.com/gsterndale/equality
Equality methodsa == ba === ba.eql? ba.equal? b
==
Everyday equality (==)a == b
Default ==a = MyBasicClass.newb = MyBasicClass.newa == b# => falseb = aa == b# => true
Overriding ==class RomanNumeral  def ==(other)    if other.respond_to?(:to_f)      self.to_f == other.to_f    else      fa...
Overriding ==iv   = RomanNumeral.new(IV)iiii = RomanNumeral.new(IIII)iv == iiii# => true
===
case statement equality (===)a === b
Default ===a = Object.newb = Object.newcase   awhen   b  b   must === aelse  b   must NOT === aend# =>   "b must NOT === a"
Float ===a = 1b = 1.0case   awhen   b  b   must === aelse  b   must NOT === aend# =>   "b must === a"
Regexp ===case 123when /d+/  At least one numberelse  No numbers foundend# => "At least one number"
When === != ==/d+/ == 123# => false/d+/ === 123# => true
Class ===case abcwhen String  It is a String!else  Not a Stringend# => "It is a String!"
Asymmetry/d+/ === 123# => true123 === /d+/# => false
Asymmetry2 === Integer# => falseInteger === 2# => trueFixnum === 2# => true
.equal?
Object equality (.equal?)a = FOOb = aa.equal? b# => truea.equal? FOO# => false
.eql?
Hash key equality (.eql?)a.eql? b
Default .eql?foo = Object.newhash = { foo => My value }bar = Object.newfoo.equal? bar# => falsefoo.eql? bar# => falsehash[...
String .eql?foo = My Keyhash = { foo => My value }bar = My Keyfoo.equal? bar# => falsefoo.eql? bar# => truehash[bar]# => "...
Overriding .eql?class RomanNumeral  def eql?(other)    other.kind_of?(RomanNumeral) &&      self.to_i.eql?(other.to_i)  en...
Overriding .eql?iv   = RomanNumeral.new(IV)hash = { iv => Four }iiii = RomanNumeral.new(IIII)iv.equal? iiii# => falseiv.eq...
Comparable
Comparison methods# You must define <=>a <=> b# => -1, 0, 1 -or- nila == ba   > ba   < ba   >= ba   <= bc.between?(a, b)
Overriding <=>class RomanNumeral  def <=>(other)    if other.respond_to?(:to_f)      self.to_f <=> other.to_f    else     ...
Overriding <=>v = RomanNumeral.new(V)x = RomanNumeral.new(X)v <=> x# => -1v >= x# => falseRomanNumeral.new(VIII).between?(...
Sorting Enumerablesiv   = RomanNumeral.new(IV)iiii = RomanNumeral.new(IIII)x    = RomanNumeral.new(X)[iv, x, iiii]# => [IV...
Upcoming SlideShare
Loading in...5
×

All Objects are created .equal?

937

Published on

Understanding equality in your Ruby codez

https://github.com/gsterndale/equality

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
937
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "All Objects are created .equal?"

  1. 1. All Objects Are Created .equal? Understand equality in your Ruby codez
  2. 2. github.com/gsterndale/equality
  3. 3. Equality methodsa == ba === ba.eql? ba.equal? b
  4. 4. ==
  5. 5. Everyday equality (==)a == b
  6. 6. Default ==a = MyBasicClass.newb = MyBasicClass.newa == b# => falseb = aa == b# => true
  7. 7. Overriding ==class RomanNumeral def ==(other) if other.respond_to?(:to_f) self.to_f == other.to_f else false end endend
  8. 8. Overriding ==iv = RomanNumeral.new(IV)iiii = RomanNumeral.new(IIII)iv == iiii# => true
  9. 9. ===
  10. 10. case statement equality (===)a === b
  11. 11. Default ===a = Object.newb = Object.newcase awhen b b must === aelse b must NOT === aend# => "b must NOT === a"
  12. 12. Float ===a = 1b = 1.0case awhen b b must === aelse b must NOT === aend# => "b must === a"
  13. 13. Regexp ===case 123when /d+/ At least one numberelse No numbers foundend# => "At least one number"
  14. 14. When === != ==/d+/ == 123# => false/d+/ === 123# => true
  15. 15. Class ===case abcwhen String It is a String!else Not a Stringend# => "It is a String!"
  16. 16. Asymmetry/d+/ === 123# => true123 === /d+/# => false
  17. 17. Asymmetry2 === Integer# => falseInteger === 2# => trueFixnum === 2# => true
  18. 18. .equal?
  19. 19. Object equality (.equal?)a = FOOb = aa.equal? b# => truea.equal? FOO# => false
  20. 20. .eql?
  21. 21. Hash key equality (.eql?)a.eql? b
  22. 22. Default .eql?foo = Object.newhash = { foo => My value }bar = Object.newfoo.equal? bar# => falsefoo.eql? bar# => falsehash[bar]# => nil
  23. 23. String .eql?foo = My Keyhash = { foo => My value }bar = My Keyfoo.equal? bar# => falsefoo.eql? bar# => truehash[bar]# => "My value"
  24. 24. Overriding .eql?class RomanNumeral def eql?(other) other.kind_of?(RomanNumeral) && self.to_i.eql?(other.to_i) endend
  25. 25. Overriding .eql?iv = RomanNumeral.new(IV)hash = { iv => Four }iiii = RomanNumeral.new(IIII)iv.equal? iiii# => falseiv.eql? iiii# => truehash[iiii]# => "Four"
  26. 26. Comparable
  27. 27. Comparison methods# You must define <=>a <=> b# => -1, 0, 1 -or- nila == ba > ba < ba >= ba <= bc.between?(a, b)
  28. 28. Overriding <=>class RomanNumeral def <=>(other) if other.respond_to?(:to_f) self.to_f <=> other.to_f else nil end endend
  29. 29. Overriding <=>v = RomanNumeral.new(V)x = RomanNumeral.new(X)v <=> x# => -1v >= x# => falseRomanNumeral.new(VIII).between?(v, x)# => true
  30. 30. Sorting Enumerablesiv = RomanNumeral.new(IV)iiii = RomanNumeral.new(IIII)x = RomanNumeral.new(X)[iv, x, iiii]# => [IV, X, IIII][iv, x, iiii].sort# => [IV, IIII, X]
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×