I read about the differences between eql? and == in ruby, and I understand that == compares the values, and eql? compares value and type
In accordance with ruby documents:
For objects of class Object, eql? is synonymous with ==. Subclasses usually continue this tradition, but there are exceptions.
It doesn't seem like the behavior indicated in the documents is automatically inherited, but this is just a suggestion on how to implement these methods. eql? this also mean that if you override either == or eql? then you have to override both?
In the Person class below, is this a typical way to override eql? and == , where the less restrictive == just delegates the more restrictive eql? (it would seem that back would be eql? delegate to == if == implemented only for comparing values, not types).
class Person def initialize(name) @name = name end def eql?(other) other.instance_of?(self.class) && @name == other.name end def ==(other) self.eql?(other) end protected attr_reader :name end
source share