On this page
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 unlessselfis infinite or not a number. -
infinite?: Returns -1,nilor +1, depending on whetherselfis-Infinity<tt>, finite, or <tt>+Infinity. -
integer?: Returns whetherselfis an integer. -
negative?: Returns whetherselfis negative. -
nonzero?: Returns whetherselfis not zero. -
positive?: Returns whetherselfis positive. -
real?: Returns whetherselfis a real value. -
zero?: Returns whetherselfis 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 whetherselfand the given value have the same value and type.
Converting
-
%(aliased asmodulo): Returns the remainder ofselfdivided by the given value. -
-@: Returns the value ofself, negated. -
abs(aliased asmagnitude): Returns the absolute value ofself. -
abs2: Returns the square ofself. -
angle(aliased asargandphase): Returns 0 ifselfis positive,Math::PIotherwise. -
ceil: Returns the smallest number greater than or equal toself, to a given precision. -
coerce: Returns array[coerced_self, coerced_other]for the given other value. -
conj(aliased asconjugate): Returns the complex conjugate ofself. -
denominator: Returns the denominator (always positive) of theRationalrepresentation ofself. -
div: Returns the value ofselfdivided by the given value and converted to an integer. -
divmod: Returns array[quotient, modulus]resulting from dividingselfthe given divisor. -
fdiv: Returns theFloatresult of dividingselfby the given divisor. -
floor: Returns the largest number less than or equal toself, to a given precision. -
i: Returns theComplexobjectComplex(0, self). the given value. -
imaginary(aliased asimag): Returns the imaginary part of theself. -
numerator: Returns the numerator of theRationalrepresentation ofself; has the same sign asself. -
polar: Returns the array[self.abs, self.arg]. -
quo: Returns the value ofselfdivided by the given value. -
real: Returns the real part ofself. -
rect(aliased asrectangular): Returns the array[self, 0]. -
remainder: Returnsself-arg*(self/arg).truncatefor the givenarg. -
round: Returns the value ofselfrounded to the nearest value for the given a precision. -
to_int: Returns theIntegerrepresentation ofself, truncating if necessary. -
truncate: Returnsselftruncated (toward zero) to a given precision.