loading...

Ruby's Comparable module

sanemane profile image Sandip Mane ・1 min read

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

Discussion

markdown guide