DEV Community

Sandip Mane
Sandip Mane

Posted on

Ruby's Comparable module

Ruby’s Comparable module is used when implementing class objects needs to be ordered or compared.

It is also used by ruby’s Enumerable module for ordering the collection. Read the blog about enumerable module.

<=>

The class must define the <=> operator, which compares the receiver against another object, returning a value less than 0, 0 or greater than 0. When the other object is not comparable, it should return nil.

coerce

Coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator.


Follow this post for more details and an example

https://www.sandipmane.dev/comparable-module-in-ruby

Top comments (0)