Skip to content
On this page

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class · Ruby 3.4

Numeric

< Object include Comparable 46 instance methods

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.

Example
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.

Example
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.

Example
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:

Here, class Numeric provides methods for:

Querying

  • finite?: Returns true unless self is infinite or not a number.

  • infinite?: Returns -1, nil or +1, depending on whether self is -Infinity<tt>, finite, or <tt>+Infinity.

  • integer?: Returns whether self is an integer.

  • negative?: Returns whether self is negative.

  • nonzero?: Returns whether self is not zero.

  • positive?: Returns whether self is positive.

  • real?: Returns whether self is a real value.

  • zero?: Returns whether self is zero.

Comparing

  • <=>: Returns:

    • -1 if self is less than the given value.

    • 0 if self is equal to the given value.

    • 1 if self is greater than the given value.

    • nil if self and the given value are not comparable.

  • eql?: Returns whether self and the given value have the same value and type.

Converting

  • % (aliased as modulo): Returns the remainder of self divided by the given value.

  • -@: Returns the value of self, negated.

  • abs (aliased as magnitude): Returns the absolute value of self.

  • abs2: Returns the square of self.

  • angle (aliased as arg and phase): Returns 0 if self is positive, Math::PI otherwise.

  • 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.

  • conj (aliased as conjugate): Returns the complex conjugate of self.

  • denominator: Returns the denominator (always positive) of the Rational representation of self.

  • div: Returns the value of self divided by the given value and converted to an integer.

  • divmod: Returns array [quotient, modulus] resulting from dividing self the given divisor.

  • fdiv: Returns the Float result of dividing self by the given divisor.

  • floor: Returns the largest number less than or equal to self, to a given precision.

  • i: Returns the Complex object Complex(0, self). the given value.

  • imaginary (aliased as imag): Returns the imaginary part of the self.

  • numerator: Returns the numerator of the Rational representation of self; has the same sign as self.

  • polar: Returns the array [self.abs, self.arg].

  • quo: Returns the value of self divided by the given value.

  • real: Returns the real part of self.

  • rect (aliased as rectangular): Returns the array [self, 0].

  • remainder: Returns self-arg*(self/arg).truncate for the given arg.

  • round: Returns the value of self rounded to the nearest value for the given a precision.

  • to_c: Returns the Complex representation of self.

  • to_int: Returns the Integer representation of self, truncating if necessary.

  • truncate: Returns self truncated (toward zero) to a given precision.

Other

  • clone: Returns self; does not allow freezing.

  • dup (aliased as +@): Returns self.

  • step: Invokes the given block with the sequence of specified numbers.

Instance methods 46