midRuby

What are the differences between standard equality operators (==, ===, eql?, equal?)?

Updated May 17, 2026

Short answer

== checks for structural value equality. === is the case equality operator used in case/when structures. eql? checks both value equality and strict type equality. equal? checks for identical object memory IDs.

Deep explanation

== is the generic equality operator, overridden by classes to compare values (e.g., 1 == 1.0 is true). === is commonly called the triple-equals or case operator; it checks if the right side 'belongs to' or is a member of the left-side condition (e.g., Integer === 1 is true). eql? is stricter; it is used by Hash to determine key matching and checks both value and type (1.eql?(1.0) is false). equal? is the absolute strictest, checking pointer identity (a.equal?(b) is true only if a.object_id == b.object_id).

Real-world example

Using === indirectly via case statements to pattern match classes or range boundaries safely. Using eql? internally when building robust collection deduplication keys.

Common mistakes

  • Using `equal?` when you want to compare if two distinct strings hold the same characters, causing tests to fail because they have separate memory object IDs.

Follow-up questions

  • How does Regex implement the `===` operator?
  • Can you override the `equal?` method?

More Ruby interview questions

View all →