Object Equality in Ruby

The == Operator


1 == 1.0

evaluates to true. The Fixnum and Float objects have equal values. Type conversion is performed by Ruby.


'foo' == 'foo'

also evaluates to true. The two distinct String objects have equal values. Most standard Ruby classes define the == operator to implement the equality.

The equal? method

This method is used to check if two objects are the same. This:


returns false. Because even though the content is the same, the objects are different. When the objects are the same:

a = b = 'foo' 


It returns true.

The eql? method

No type conversion is performed. So:


prints false.

The === operator

The case equality operator is used in the switch-case statements. Range and Regexp defines this operator that makes sense for the class. It is not common to use this operator explicitly.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.