On this page
End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Integer
An Integer object represents an integer value.
You can create an Integer object explicitly with:
-
An integer literal.
You can convert certain objects to Integers with:
-
Method Integer.
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:
-
Inherits from class Numeric.
Here, class Integer provides methods for:
Querying
allbits?-
Returns whether all bits in
selfare set.
anybits?-
Returns whether any bits in
selfare set.
nobits?-
Returns whether no bits in
selfare set.
Comparing
- <
-
Returns whether
selfis less than the given value.
- <=
-
Returns whether
selfis less than or equal to the given value.
- <=>
-
Returns a number indicating whether
selfis less than, equal to, or greater than the given value.
- >
-
Returns whether
selfis greater than the given value.
- >=
-
Returns whether
selfis 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.
- &
-
Returns the bitwise AND of
selfand the given value.
*-
Returns the product of
selfand the given value.
- **
-
Returns the value of
selfraised to the power of the given value.
+-
Returns the sum of
selfand the given value.
--
Returns the difference of
selfand the given value.
- /
-
Returns the quotient of
selfand the given value.
<<-
Returns the value of
selfafter a leftward bit-shift.
>>-
Returns the value of
selfafter a rightward bit-shift.
[]-
Returns a slice of bits from
self.
- ^
-
Returns the bitwise EXCLUSIVE OR of
selfand 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
selfby the given value.
divmod-
Returns a 2-element array containing the quotient and remainder results of dividing
selfby 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
selfby the given value.
round-
Returns
selfrounded to the nearest value with the given precision.
truncate-
Returns
selftruncated to the given precision.
- /
-
Returns the bitwise OR of
selfand the given value.
Other
Class methods 2
Instance methods 66
The version of loaded GMP.
Ruby version changes 1
-
3.1
-
Integer.try_convertis added. [Feature #15211]
-