I have a library class, which provides methods for equality and inequality. I'm deriving another class from this, which, differently from the parent class, introduces an ordering relation, i.e. it would make sense to ask for two elements of the derived class, which one is smaller. In particular, arrays of objects of the derived class can be sorted.
My first approach was
class MyClass < LibraryClass
return 0 if self == other
return -1 if self < other
# code for operators > <= >= is not shown here....
self == other
Comparable the superclass of
LibraryClass the superclass of
Comparable. So, the implementation of
Comparable overrides the implementation of
What you can then do, is to override
== again in
MyClass with a version that is identical to the one in
class MyClass < LibraryClass include Comparable def <=>(other) # whatever end define_method(:==, LibraryClass.public_instance_method(:==)) end