---
title: "Implementing Equality in Ruby"
description: "Ruby distinguishes several forms of equality (#==, #equal?, #eql?, #hash, #===), and each needs its own correct implementation in custom classes."
url: "https://swe.quest/articles/implementing-equality-in-ruby-f99e6a9b20284d2f"
canonical: "https://shopify.engineering/implementing-equality-in-ruby"
source: "Shopify Engineering"
published: "2022-05-26"
difficulty: "medium"
kind: "How-to"
reading_minutes: 18
tags: ["ruby","language-design"]
---

# Implementing Equality in Ruby

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-05-26, 18 min read, How-to. Original: [https://shopify.engineering/implementing-equality-in-ruby](https://shopify.engineering/implementing-equality-in-ruby)

## Summary

Ruby distinguishes several forms of equality (#==, #equal?, #eql?, #hash, #===), and each needs its own correct implementation in custom classes. Value objects should compare all attributes, while entities should compare only their ID, with super ensuring reflexivity and nil IDs never matching. Correct implementations must preserve reflexivity, symmetry and transitivity, plus the invariants linking equal?, eql?, == and hash. Practical advice: put the logic in #eql? and have #== delegate to it unless type coercion via #to\_str is needed, never override #equal?, and avoid XOR when combining hash codes.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Language design](https://swe.quest/tags/language-design.md) (Compilers & runtimes)

## Prerequisites

- Ruby classes and methods ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- Hash tables basics

## Keywords

Ruby, ActiveRecord, IEEE 754, Domain-driven design

## Related posts

- [Changing a polymorphic\_type in Rails](https://swe.quest/articles/changing-a-polymorphic-type-in-rails-8b5924e77ba397de.md): Shopify Engineering · 2022-02-18 · Medium · 7 min read · Ruby, Refactoring & migrations, Schema migrations, Databases
  Rails stores the parent's class name in a polymorphic association's type column, so moving models into a new namespace (e.g. Car to Garage::Car) leaves mixed values like "Car" and "Garage::Car" and breaks lookups in both directions.
- [Focus On Behavior, Not State, for a More Maintainable Codebase](https://swe.quest/articles/focus-on-behavior-not-state-for-a-more-maintainable-codebase-d186fd1356391a16.md): Shopify Engineering · 2022-09-01 · Medium · 9 min read · Refactoring & migrations, Ruby, E-commerce, System design
  When adding pre-orders and try-before-you-buy to Shopify's Purchase Options APIs, code that equated "has a selling plan" with "is a subscription" forced repeated refactors.
- [Caching Without Marshal Part 1: Marshal from the Inside Out](https://swe.quest/articles/caching-without-marshal-part-1-marshal-from-the-inside-out-57f413ca18174072.md): Shopify Engineering · 2022-11-11 · Medium · 11 min read · Ruby, Caching, Runtimes & VMs
  Ruby's Marshal serializes an object's full class graph into cache entries, so a refactor that renamed classes caused old and new code to choke on each other's cached beta-flag objects mid-deploy and triggered an incident at Shopify.
- [Type Constraints in 65 lines of SQL](https://swe.quest/articles/type-constraints-in-65-lines-of-sql-c09c12befbef6b57.md): Supabase · 2023-02-17 · Medium · 6 min read · PostgreSQL, SQL, Type systems
  A Postgres composite type plus a domain with check constraints can model Semantic Versioning so invalid versions are rejected on insert or cast.
- [The Write Last, Read First Rule](https://swe.quest/articles/the-write-last-read-first-rule-6fa0042a5ce38b5c.md): TigerBeetle · 2025-11-06 · Medium · 8 min read · Consistency models, Distributed systems, Payments & fintech, Resilience, TypeScript
  When an application spans two stores without a shared transaction boundary, such as Postgres for account master data and TigerBeetle for the ledger, one must be designated the system of record and the other the system of reference.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
