Equality and value objects

The equality set

Three methods together decide what "the same value" means: == for a comparison a reader writes, eql? and hash for the lookup a Hash and a Set do. A class that defines only the first is equal in a test and different in a set:

class Money attr_reader :cents def initialize(cents) @cents = cents end def ==(other) other.is_a?(Money) && cents == other.cents end end first = Money.new(100) second = Money.new(100) p first == second p first.eql?(second) p Set[first, second].size

ruby eq.rb

true
false
2

The two values are equal and the set holds both of them, because the set never calls ==. That is the bug the next two sections fix.

A value object without the work

Data.define gives all three methods from the fields, freezes the value and computes a hash from the fields. It is a hash key and a set member with no extra code:

ruby -e 'A = Data.define(:v); p({A.new(v: 1) => :x}[A.new(v: 1)]); p A.new(v: 1) == A.new(v: 1); p A.new(v: 1).hash == A.new(v: 1).hash'

:x
true
true

The lookup found the value under a different instance, which is the whole purpose of a value object: two instances with the same fields are interchangeable. The frozen state is part of it too, because a key that can change its hash stops being findable.

A class that defines only ==

When a hand-written class needs the same behaviour, the pattern is a comparison plus two aliases:

class Money attr_reader :cents def initialize(cents) @cents = cents freeze end def ==(other) other.is_a?(Money) && cents == other.cents end alias eql? == def hash [Money, cents].hash end end p Set[Money.new(100), Money.new(100)].size p({ Money.new(100) => :x }[Money.new(100)])

ruby eq2.rb

1
:x

The hash includes the class, so a Money and an Integer with the same number do not collide. freeze in the constructor is what keeps the hash stable for the life of the object.

What to watch for

alias eql? == is only correct when the comparison is strict about the class, as above. If == accepts a plain number for convenience, the alias makes 1.eql?(Money.new(1)) depend on the order of the arguments.

A mutable field in a value object breaks its hash. Freeze the object, or keep the fields that feed hash immutable even when the rest of the object can change.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which three methods decide equality for a value object?

    Answer

    ==, eql? and hash

    == is the comparison a reader writes; eql? and hash are what a Hash and a Set look up with.

  2. What does this program print? class Money attr_reader :cents def initialize(cents) @cents = cents end def ==(other) other.is_a?(Money) && cents == other.cents end end first = Money.new(100) second = Money.new(100) p first == second p first.eql?(second) p Set[first, second].size

    Answer

    true / false / 2

    Only == is defined, so the set sees two different members.

  3. What does this program print? A = Data.define(:v) p({A.new(v: 1) => :x}[A.new(v: 1)])

    Answer

    :x

    A Data value hashes and compares by its fields, so a second instance finds the key.

  4. What does this program print? A = Data.define(:v) p A.new(v: 1) == A.new(v: 1)

    Answer

    true

    Data compares by value: two instances with the same fields are equal.

  5. What does a Set or a Hash use to find a key?

    Answer

    eql? and hash

    That is why a class that defines only == collects duplicates in a set.

  6. What does this program print? class Money attr_reader :cents def initialize(cents) @cents = cents end def ==(other) other.is_a?(Money) && cents == other.cents end alias eql? == end p({Money.new(100) => :x}[Money.new(100)])

    Answer

    nil

    Two objects with the same fields have different object hashes until hash is defined, so the lookup misses and answers nil.

  7. Why does the constructor of a hand-written value object call freeze?

    Answer

    A key that can change its hash stops being findable

    A frozen object cannot change the fields its hash is computed from.

  8. What does this program print? class Money attr_reader :cents def initialize(cents) @cents = cents end def ==(other) other.is_a?(Money) && cents == other.cents end end p Money.new(100) == Money.new(100)

    Answer

    true

    == compares the cents, so two separate objects with 100 are equal.

  9. What must hash be consistent with?

    Answer

    eql?: two values that are eql? must have the same hash

    A hash table looks a key up by hash first and then compares with eql?.

  10. A value object has a mutable field that feeds hash. What goes wrong?

    Answer

    The object can no longer be found under the key after the field changes

    The stored hash no longer matches the object, so a lookup misses it.

Article author: Arthur Isaev