On this page
End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Numeric
Numeric is the class from which all higher-level numeric classes should inherit.
Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as Integer are implemented as immediates, which means that each Integer is a single immutable object which is always passed by value.
a = 1 1.object_id == a.object_id #=> true
There can only ever be one instance of the integer 1, for example. Ruby ensures this by preventing instantiation. If duplication is attempted, the same instance is returned.
Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class 1.dup #=> 1 1.object_id == 1.dup.object_id #=> true
For this reason, Numeric should be used when defining other numeric classes.
Classes which inherit from Numeric must implement coerce, which returns a two-member Array containing an object that has been coerced into an instance of the new class and self (see coerce).
Inheriting classes should also implement arithmetic operator methods (+, -, * and /) and the <=> operator (see Comparable). These methods may rely on coerce to ensure interoperability with instances of other numeric classes.
class Tally < Numeric def initialize(string) @string = string end def to_s @string end def to_i @string.size end def coerce(other) [self.class.new('|' * other.to_i), self] end def <=>(other) to_i <=> other.to_i end def +(other) self.class.new('|' * (to_i + other.to_i)) end def -(other) self.class.new('|' * (to_i - other.to_i)) end def *(other) self.class.new('|' * (to_i * other.to_i)) end def /(other) self.class.new('|' * (to_i / other.to_i)) end end tally = Tally.new('||') puts tally * 2 #=> "||||" puts tally > 1 #=> true
What’s Here
First, what’s elsewhere. Class Numeric:
-
Inherits from class Object.
-
Includes module Comparable.
Here, class Numeric provides methods for:
Querying
finite?-
Returns true unless
selfis infinite or not a number.
infinite?-
Returns -1,
nilor +1, depending on whetherselfis-Infinity<tt>, finite, or <tt>+Infinity.
integer?-
Returns whether
selfis an integer.
negative?-
Returns whether
selfis negative.
nonzero?-
Returns whether
selfis not zero.
positive?-
Returns whether
selfis positive.
real?-
Returns whether
selfis a real value.
zero?-
Returns whether
selfis zero.
Comparing
- <=>
-
Returns:
-
-1 if
selfis less than the given value. -
0 if
selfis equal to the given value. -
1 if
selfis greater than the given value. -
nilifselfand the given value are not comparable.
eql?-
Returns whether
selfand the given value have the same value and type.
Converting
-@-
Returns the value of
self, negated.
abs2-
Returns the square of
self.
ceil-
Returns the smallest number greater than or equal to
self, to a given precision.
coerce-
Returns array
[coerced_self, coerced_other]for the given other value.
denominator-
Returns the denominator (always positive) of the
Rationalrepresentation ofself.
div-
Returns the value of
selfdivided by the given value and converted to an integer.
divmod-
Returns array
[quotient, modulus]resulting from dividingselfthe given divisor.
floor-
Returns the largest number less than or equal to
self, to a given precision.
polar-
Returns the array
[self.abs, self.arg].
quo-
Returns the value of
selfdivided by the given value.
real-
Returns the real part of
self.
rect(aliased asrectangular)-
Returns the array
[self, 0].
remainder-
Returns
self-arg*(self/arg).truncatefor the givenarg.
round-
Returns the value of
selfrounded to the nearest value for the given a precision.
truncate-
Returns
selftruncated (toward zero) to a given precision.