Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
192: Object Equality: ==, eql?, equal?
A few years ago, I was reviewing a PR for a junior dev who was building a caching layer for a pricing engine. He had a Hash where the keys were product IDs, and he was trying to retrieve a cached price using a variable he thought was a matching ID. The logic looked perfect: if cache[id]. However, in production, the cache was missing hits constantly. It turned out he was occasionally passing the ID as a Float (e.g., 101.0) while the keys in the Hash were Integers (101). He told me, "But I checked it with == and it returned true!" He was right—101 == 101.0 is true in Ruby—but the Hash didn't care about ==. It used something else entirely.
Value Equality and the Flexibility of ==
When you use ==, you're generally asking, "Do these two objects represent the same value?" In Ruby, == is a method that can be overridden by almost any class to define what "equality" means for that specific type of object. This is why it's so flexible. For example, if you compare two different string objects that happen to contain the same characters, == returns true because the content is the same.
str1 = "ruby"
str2 = "ruby"
puts str1 == str2 # => true
I usually tell people to treat == as the "intuitive" check. It's the one you'll use 95% of the time. However, because it's so easily overridden, it can sometimes be too permissive, which leads us to the next level of strictness.
The Strictness of eql? and the Hash Connection
The eql? method is similar to ==, but it's designed to be more rigorous. Its primary purpose in the Ruby ecosystem is to serve as the basis for Hash key lookups. For most classes, eql? behaves exactly like ==, but for numeric types, it's a different story. eql? requires that the objects not only have the same value but also be of the same class.
1 == 1.0 # => true
1.eql?(1.0) # => false
This explains the bug from my anecdote. When you put a key into a Hash, Ruby doesn't use == to find it later; it uses eql? (and the object's hash method). If you use an Integer as a key, you cannot retrieve that value using a Float, even if they are mathematically equivalent. If you're building a system where types might be mixed—like data coming from a JSON API where numbers might arrive as floats—be very careful about which equality method is being invoked under the hood.
Identity and the Absolute Certainty of equal?
Finally, we have equal?. This is the strictest check of all. It doesn't care about values or classes; it only cares about identity. It asks: "Are these two variables pointing to the exact same object in memory?"
a = "hello"
b = "hello"
c = a
puts a.equal?(b) # => false (different objects, same value)
puts a.equal?(c) # => true (exact same object)
You won't use equal? often in daily business logic, but it's an indispensable tool for debugging. If you suspect that a method is mutating an object instead of returning a new copy, equal? is how you prove it. It essentially compares the object_id of the two instances. If the IDs match, they are the same piece of memory, period.
📋 Practical Task
The Strict Identity and Value Validator
You are tasked with writing a diagnostic method for a library that handles sensitive data. You need to create a method called compare_objects(obj1, obj2) that returns a specific string describing the relationship between two inputs.
The method should follow these rules:
- Return
"Identical"if the objects are the exact same instance in memory (usingequal?). - Return
"Equivalent"if the objects have the same value but are different instances or different types (using==). - Return
"Different"if neither of the above is true.
Test your method with these cases:
# Case 1: Identical
x = [1, 2, 3]
y = x
compare_objects(x, y) # Should return "Identical"
# Case 2: Equivalent
a = "Ruby"
b = "Ruby"
compare_objects(a, b) # Should return "Equivalent"
# Case 3: Equivalent (but different types)
compare_objects(10, 10.0) # Should return "Equivalent"
# Case 4: Different
compare_objects("Ruby", "Python") # Should return "Different"
There are no comments for now.