DEV Community

Sandip Mane
Sandip Mane

Posted on

1 2

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more