On this page
Preview Ruby 4.1 is a preview. It can change before the final release. 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 and class Object.
-
Includes module Comparable.
Here, class Integer provides methods for:
Querying
-
allbits?: Returns whether all bits inselfare set. -
anybits?: Returns whether any bits inselfare set. -
nobits?: Returns whether no bits inselfare set.
Comparing
-
<: Returns whetherselfis less than the given value. -
<=: Returns whetherselfis less than or equal to the given value. -
<=>: Returns a number indicating whetherselfis less than, equal to, or greater than the given value. -
==(aliased as===): Returns whetherselfis equal to the givenvalue.
-
>: Returns whetherselfis greater than the given value. -
>=: Returns whetherselfis 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 ofselfand the given value. -
*: Returns the product ofselfand the given value. -
**: Returns the value ofselfraised to the power of the given value. -
+: Returns the sum ofselfand the given value. -
-: Returns the difference ofselfand the given value. -
/: Returns the quotient ofselfand the given value. -
<<: Returns the value ofselfafter a leftward bit-shift. -
>>: Returns the value ofselfafter a rightward bit-shift. -
[]: Returns a slice of bits fromself. -
^: Returns the bitwise EXCLUSIVE OR ofselfand the given value. -
|: Returns the bitwise OR ofselfand the given value. -
ceil: Returns the smallest number greater than or equal toself. -
chr: Returns a 1-character string containing the character represented by the value ofself. -
digits: Returns an array of integers representing the base-radix digits ofself. -
div: Returns the integer result of dividingselfby the given value. -
divmod: Returns a 2-element array containing the quotient and remainder results of dividingselfby the given value. -
fdiv: Returns theFloatresult of dividingselfby the given value. -
floor: Returns the greatest number smaller than or equal toself. -
pow: Returns the modular exponentiation ofself. -
pred: Returns the integer predecessor ofself. -
remainder: Returns the remainder after dividingselfby the given value. -
round: Returnsselfrounded to the nearest value with the given precision. -
succ(aliased asnext): Returns the integer successor ofself. -
to_s(aliased asinspect): Returns a string containing the place-value representation ofselfin the given radix. -
truncate: Returnsselftruncated to the given precision.
Other
Class methods 2
Instance methods 67
The version of loaded GMP.
Ruby version changes 4
-
4.1
-
Integer#bit_countis added. It returns the number of1bits in the binary representation of a non-negative integer (its population count). [Feature #20163]
-
-
3.4
-
Integer#**used to returnFloat::INFINITYwhen the return value is large, but now returns anInteger. If the return value is extremely large, it raises an exception. [Feature #20811]
-
-
3.2
-
Integer#ceildivhas been added. [Feature #18809]
-
-
3.1
-
Integer.try_convertis added. [Feature #15211]
-