BlogsShopifyRuby Equality Implementation

Ruby Equality Implementation

Ruby Equality Implementation

2
posts
2022–2023

This feature thread tracks the development and enhancement of how equality is implemented and understood within Ruby at Shopify. Initial efforts focused on understanding the nuances of Ruby's equality operators (#==, #eql?, #equal?, #===) and how to correctly implement them for custom objects. This includes differentiating between entities and value objects, and handling type coercion. This post details the dangers of monkey patching, advocating for upstream contributions and avoiding it where possible, and also discusses refinements as a slower alternative. Future development may explore advanced equality concepts, performance implications, and best practices for ensuring safe and maintainable code.

2023

The Case Against Monkey Patching, From a Rails Core Team Member - Shopify

2/21/2023

This post argues against the use of monkey patching in Ruby, highlighting its dangers including difficulty in upgrades, security vulnerabilities, increased technical debt, and negative impact on open-source communities. It advocates for upstream contributions and provides alternatives like prioritizing upgrades or fixing bugs upstream. The post also discusses refinements, noting their performance drawbacks. The author shares an example of a monkey patch for MySQL query warnings that was eventually upstreamed into Rails.

2022

Implementing Equality in Ruby - Shopify

5/26/2022

This post details the fundamental concepts of equality in Ruby, including the different equality operators (#==, #eql?, #equal?, #===) and their implementations. It explains the importance of correctly implementing equality for custom objects, differentiating between entities (identity-based equality) and value objects (attribute-based equality). The post provides code examples for implementing #== for both types, discusses the properties of equality (reflexivity, symmetry, transitivity), and touches upon type coercion and the #to_str method. It also briefly mentions the Ship of Theseus thought experiment in the context of entity identity.