Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

All Objects are created .equal?

1,199 views

Published on

Understanding equality in your Ruby codez

https://github.com/gsterndale/equality

  • Be the first to comment

  • Be the first to like this

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]

×