Should the implementation of "==" in Ruby check the type?

I want to implement ==for the ruby ​​class. I can do

def ==(o)
  o.respond_to?(:id) && self.id == o.id
end

or

def ==(o)
  o.is_a?(Foo) && self.id == o.id
end

According to this article, it seems that the first meaning makes sense. If I implemented eql?, I would do the latter. It's right?

+5
source share
1 answer

It depends on whether you are being compared with any object or with a particular type. The second form is specific, the first is general.

, , . , , , , . id . , Foo 10 Bar 10 , .

+1

All Articles