Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Shopify Engineering 18 min read How-to mediumAdept

Implementing Equality in Ruby

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.

Assumes: Ruby classes and methodsHash tables basics

Related posts

  • Changing a polymorphic_type in Rails

    Shopify Engineering mediumAdept

    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. Shopify's Payment Flexibility team decoupled the database from class names by overriding polymorphic_name to write arbitrary strings such as "car", then used an unscoped association scope to read both old and new values during the transition. After a backfill of existing rows, the temporary scope is removed and future renames only require updating a class-mapping hash. The approach adds complexity and is mainly worth it when module and class names are expected to change.

  • Focus On Behavior, Not State, for a More Maintainable Codebase

    Shopify Engineering mediumAdept

    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. The fix was to model behavior rather than state: wrap selling plans in an object that answers questions like "has recurring deliveries?" or "is billing deferred?" (upfront/deferred/recurring × billing/delivery), so payments, delivery and inventory code ask only what they need and conditionals disappear. To shield the data team from schema churn, a separate reporting table records these traits and labels per order, acting as a translation layer between platform code and merchant-facing domain concepts.

  • Caching Without Marshal Part 1: Marshal from the Inside Out

    Shopify Engineering mediumAdept

    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. Walking through marshal.c and real payloads, the post explains how Marshal encodes objects (TYPE_OBJECT), instance variables on core types (TYPE_IVAR), circular references (TYPE_LINK) and subclasses of String/Array/Hash/Regex (TYPE_UCLASS). These are exactly the features that break when replacing Marshal, setting up Shopify's migration of Rails cache serialization to MessagePack in part two.

  • Type Constraints in 65 lines of SQL

    Supabase mediumAdept

    A Postgres composite type plus a domain with check constraints can model Semantic Versioning so invalid versions are rejected on insert or cast. A semver_components composite stores major, minor, patch, pre-release and build metadata as queryable fields, and a semver domain adds non-null and non-negative checks and regex validation of array elements through a helper SQL function. An explicit semver_to_text function, optionally persisted as a stored generated column, gives the familiar 2.0.0-rc.1 rendering. The approach suits types whose integrity is critical and whose spec is stable, and it can be extended with custom equality operators, parsers and aggregates.

  • The Write Last, Read First Rule

    TigerBeetle mediumAdept

    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. Writes go to the system of reference first and the system of record last, and existence checks read the system of record first. This keeps the system traceable: no funded ledger account ever lacks an owner record, even if it is temporarily inconsistent. The approach is implemented with Resonate's durable execution (Distributed Async Await) and idempotent per-store create functions. A result matrix classifies outcomes as success, recovery, conflict or ordering violation, and the application panics to alert an operator on violations.

The quest for software engineering knowledge.

Look:
Layout: