Skip to content
On this page

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class · Ruby 3.1

Integer

< Numeric 66 instance methods 2 class methods

An Integer object represents an integer value.

You can create an Integer object explicitly with:

You can convert certain objects to Integers with:

An attempt to add a singleton method to an instance of this class causes an exception to be raised.

What’s Here

First, what’s elsewhere. Class Integer:

Here, class Integer provides methods for:

Querying

  • allbits?

    Returns whether all bits in self are set.

  • anybits?

    Returns whether any bits in self are set.

  • nobits?

    Returns whether no bits in self are set.

Comparing

  • <

    Returns whether self is less than the given value.

  • <=

    Returns whether self is less than or equal to the given value.

  • <=>

    Returns a number indicating whether self is less than, equal to, or greater than the given value.

  • == (aliased as ===)

    Returns whether self is equal to the given value.

  • >

    Returns whether self is greater than the given value.

  • >=

    Returns whether self is greater than or equal to the given value.

Converting

  • ::sqrt

    Returns the integer square root of the given value.

  • ::try_convert

    Returns the given value converted to an Integer.

  • % (aliased as modulo)

    Returns self modulo the given value.

  • &

    Returns the bitwise AND of self and the given value.

  • *

    Returns the product of self and the given value.

  • **

    Returns the value of self raised to the power of the given value.

  • +

    Returns the sum of self and the given value.

  • -

    Returns the difference of self and the given value.

  • /

    Returns the quotient of self and the given value.

  • <<

    Returns the value of self after a leftward bit-shift.

  • >>

    Returns the value of self after a rightward bit-shift.

  • []

    Returns a slice of bits from self.

  • ^

    Returns the bitwise EXCLUSIVE OR of self and the given value.

  • ceil

    Returns the smallest number greater than or equal to self.

  • chr

    Returns a 1-character string containing the character represented by the value of self.

  • digits

    Returns an array of integers representing the base-radix digits of self.

  • div

    Returns the integer result of dividing self by the given value.

  • divmod

    Returns a 2-element array containing the quotient and remainder results of dividing self by the given value.

  • fdiv

    Returns the Float result of dividing self by the given value.

  • floor

    Returns the greatest number smaller than or equal to self.

  • pow

    Returns the modular exponentiation of self.

  • pred

    Returns the integer predecessor of self.

  • remainder

    Returns the remainder after dividing self by the given value.

  • round

    Returns self rounded to the nearest value with the given precision.

  • succ (aliased as next)

    Returns the integer successor of self.

  • to_f

    Returns self converted to a Float.

  • to_s (aliased as inspect)

    Returns a string containing the place-value representation of self in the given radix.

  • truncate

    Returns self truncated to the given precision.

  • /

    Returns the bitwise OR of self and the given value.

Other

  • downto

    Calls the given block with each integer value from self down to the given value.

  • times

    Calls the given block self times with each integer in (0..self-1).

  • upto

    Calls the given block with each integer value from self up to the given value.

Class methods 2

Instance methods 66

Constants

The version of loaded GMP.

Ruby version changes 1

  1. 3.1