All Objects are created .equal?

30
All Objects Are Created .equal? Understand equality in your Ruby codez

description

Understanding equality in your Ruby codez https://github.com/gsterndale/equality

Transcript of All Objects are created .equal?

Page 1: All Objects are created .equal?

All Objects Are Created .equal?Understand equality in your Ruby codez

Page 3: All Objects are created .equal?

a == b

a === b

a.eql? b

a.equal? b

Equality methods

Page 4: All Objects are created .equal?

==

Page 5: All Objects are created .equal?

a == b

Everyday equality (==)

Page 6: All Objects are created .equal?

a = MyBasicClass.newb = MyBasicClass.new

a == b# => false

b = a

a == b# => true

Default ==

Page 7: All Objects are created .equal?

class RomanNumeral

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

end

Overriding ==

Page 8: All Objects are created .equal?

iv = RomanNumeral.new('IV')

iiii = RomanNumeral.new('IIII')

iv == iiii# => true

Overriding ==

Page 9: All Objects are created .equal?

===

Page 10: All Objects are created .equal?

a === b

case statement equality (===)

Page 11: All Objects are created .equal?

a = Object.newb = Object.new

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

Default ===

Page 12: All Objects are created .equal?

a = 1b = 1.0

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

Float ===

Page 13: All Objects are created .equal?

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

Regexp ===

Page 14: All Objects are created .equal?

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

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

When === != ==

Page 15: All Objects are created .equal?

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

Class ===

Page 16: All Objects are created .equal?

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

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

Asymmetry

Page 17: All Objects are created .equal?

2 === Integer# => false

Integer === 2# => true

Fixnum === 2# => true

Asymmetry

Page 18: All Objects are created .equal?

.equal?

Page 19: All Objects are created .equal?

a = 'FOO'b = a

a.equal? b# => true

a.equal? 'FOO'# => false

Object equality (.equal?)

Page 20: All Objects are created .equal?

.eql?

Page 21: All Objects are created .equal?

a.eql? b

Hash key equality (.eql?)

Page 22: All Objects are created .equal?

foo = Object.newhash = { foo => 'My value' }

bar = Object.new

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

hash[bar]# => nil

Default .eql?

Page 23: All Objects are created .equal?

foo = 'My Key'hash = { foo => 'My value' }

bar = 'My Key'

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

hash[bar]# => "My value"

String .eql?

Page 24: All Objects are created .equal?

class RomanNumeral

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

end

Overriding .eql?

Page 25: All Objects are created .equal?

iv = RomanNumeral.new('IV')

hash = { iv => 'Four' }

iiii = RomanNumeral.new('IIII')

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

hash[iiii]# => "Four"

Overriding .eql?

Page 26: All Objects are created .equal?

Comparable

Page 27: All Objects are created .equal?

# You must define <=>a <=> b# => -1, 0, 1 -or- nil

a == b

a > ba < ba >= ba <= b

c.between?(a, b)

Comparison methods

Page 28: All Objects are created .equal?

class RomanNumeral

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

end

Overriding <=>

Page 29: All Objects are created .equal?

v = RomanNumeral.new('V')x = RomanNumeral.new('X')

v <=> x# => -1

v >= x# => false

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

Overriding <=>

Page 30: All Objects are created .equal?

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]

Sorting Enumerables