12

I have a class Issue in which each class has a key field. Because keys are meant to be unique, I overrode the comparison operator such that two Issue objects are compared based on key like so:

def ==(other_issue)
  other_issue.key == @key
end

However, I am dealing with a case in which two there may be two variables referring to the same instance of an Issue, and thus a comparison by key would not distinguish between them. Is there any way I could check if the two variables refer to the same place?

1

2 Answers 2

15

According to the source, the Object#equal? method should do what you're trying to do:

// From object.c
rb_obj_equal(VALUE obj1, VALUE obj2)
{
    if (obj1 == obj2) return Qtrue;
    return Qfalse;
}

So the ruby code you would write is:

obj1.equal?(obj2)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, .equal? was exactly what I needed. thank you also for the link to the source.
When I try this with identical active record objects, they don't return true. Is there a way to use .equal? on an Active Record object?
I'm not 100% sure how equality is defined in the confines of active record. I'd expect that two records are considered the same if they refer to the same database row (i.e. if their IDs are the same, most of the time). I think for this sort of check you want to use ==, which is the same as eql? (not equal?). See also this question.
-2

No, not really. And that is Good Thing™.

A basic tenet of object-orientation is that one object can simulate another object. If you could tell whether or not two objects are the same object ("Reference Equality"), then you could tell apart the simulation from the real thing, thus breaking object-orientation … which, for an object-oriented language like Ruby is not so good.

The default implementation of Object#equal? does indeed check for reference equality, but, just like every other method in Ruby, it can be overridden in subclasses or monkey-patched, so there is no way to guarantee that it will actually check for reference equality. And that is how it should be.

1 Comment

The documentation of equal? explicitly states that it should not be overridden because it is use for object identity (in hashes and the like). Overriding equal? probably has severe side-effects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.