Equality, Comparison & Copies (Capstone)

Three kinds of "equal" β€” and when each fires

"Are these equal?" has three answers in Ruby, and mixing them up causes real bugs (duplicate hash keys, sets that don't dedupe):

  • equal? β€” identity. Same object in memory. Never override it.
  • == β€” value equality. "Do these represent the same thing?" Override this for your own value objects.
  • eql? β€” the stricter equality used by Hash keys, together with hash. Two objects that are eql? must return the same hash.

In an interview, say: "equal? is identity, == is value equality you define, and eql? + hash are the Hash-key contract. If I override == for a value object, I override eql? and hash too, or it'll behave wrong as a hash key or in a Set."