Numbers

We talked about numeric literals in Integer and Floating-Point Numbers. Here, let’s talk about the library methods for numbers.

Numeric Class

All number classes inherit from the class Numeric. If you want to define your own number subclass, it’s recommended that you also inherit from Numeric because Numeric does some internal things about storing numbers in memory that are useful to have.

It might go without saying—but it’s our job to say it—that Numeric implements Numeric#<=> and includes Comparable. As with other <=>, the return value is -1 if the left side is smaller, 1 if the left side is greater, and 0 if the two are equal. The equality test is also aliased as eql?.

It’s also our job to say that the actual arithmetic operators— ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.