class 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 given value. -
>: 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 anInteger. -
&: 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
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 128
def self.sqrt: (int n) -> Integer
Returns the integer square root of the non-negative integer n, which is the largest non-negative integer less than or equal to the square root of numeric.
Integer.sqrt(0) # => 0 Integer.sqrt(1) # => 1 Integer.sqrt(24) # => 4 Integer.sqrt(25) # => 5 Integer.sqrt(10**400) # => 10**200
If numeric is not an Integer, it is converted to an Integer:
Integer.sqrt(Complex(4, 0)) # => 2 Integer.sqrt(Rational(4, 1)) # => 2 Integer.sqrt(4.0) # => 2 Integer.sqrt(3.14159) # => 1
This method is equivalent to Math.sqrt(numeric).floor, except that the result of the latter code may differ from the true value due to the limited precision of floating point arithmetic.
Integer.sqrt(10**46) # => 100000000000000000000000 Math.sqrt(10**46).floor # => 99999999999999991611392
Raises an exception if numeric is negative.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 147
def self.try_convert: (int) -> Integer
| (untyped) -> Integer?
If object is an Integer object, returns object. Integer.try_convert(1) # => 1
Otherwise if object responds to :to_int, calls object.to_int and returns the result. Integer.try_convert(1.25) # => 1
Returns nil if object does not respond to :to_int Integer.try_convert([]) # => nil
Raises an exception unless object.to_int returns an Integer object.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 177
def %: (Float) -> Float
| (Rational) -> Rational
| (Integer) -> Integer
| (Numeric) -> Numeric
Returns self modulo other as a real numeric (Integer, Float, or Rational).
For integer n and real number r, these expressions are equivalent:
n % r n-r*(n/r).floor n.divmod(r)[1]
See Numeric#divmod.
Examples:
10 % 2 # => 0 10 % 3 # => 1 10 % 4 # => 2 10 % -2 # => 0 10 % -3 # => -2 10 % -4 # => -2 10 % 3.0 # => 1.0 10 % Rational(3, 1) # => (1/1)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 195
def &: (Integer) -> Integer
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1278
def *: (BigDecimal) -> BigDecimal
| ...
Performs multiplication:
4 * 2 # => 8 4 * -2 # => -8 -4 * 2 # => -8 4 * 2.0 # => 8.0 4 * Rational(1, 3) # => (4/3) 4 * Complex(2, 0) # => (8+0i)
Returns the numeric product of self and other:
4 * 2 # => 8 -4 * 2 # => -8 4 * -2 # => -8 4 * 2.0 # => 8.0 4 * Rational(1, 3) # => (4/3) 4 * Complex(2, 0) # => (8+0i)
Returns the numeric product of self and other:
4 * 2 # => 8 -4 * 2 # => -8 4 * -2 # => -8 4 * 2.0 # => 8.0 4 * Rational(1, 3) # => (4/3) 4 * Complex(2, 0) # => (8+0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 259
def **: (Integer) -> Numeric
| (Float) -> Numeric
| (Rational) -> Numeric
| (Complex) -> Complex
Returns self raised to the power exponent:
# Result for non-negative Integer exponent is Integer. 2 ** 0 # => 1 2 ** 1 # => 2 2 ** 2 # => 4 2 ** 3 # => 8 -2 ** 3 # => -8 # Result for negative Integer exponent is Rational, not Float. 2 ** -3 # => (1/8) -2 ** -3 # => (-1/8) # Result for Float exponent is Float. 2 ** 0.0 # => 1.0 2 ** 1.0 # => 2.0 2 ** 2.0 # => 4.0 2 ** 3.0 # => 8.0 -2 ** 3.0 # => -8.0 2 ** -3.0 # => 0.125 -2 ** -3.0 # => -0.125 # Result for non-negative Complex exponent is Complex with Integer parts. 2 ** Complex(0, 0) # => (1+0i) 2 ** Complex(1, 0) # => (2+0i) 2 ** Complex(2, 0) # => (4+0i) 2 ** Complex(3, 0) # => (8+0i) -2 ** Complex(3, 0) # => (-8+0i) # Result for negative Complex exponent is Complex with Rational parts. 2 ** Complex(-3, 0) # => ((1/8)+(0/1)*i) -2 ** Complex(-3, 0) # => ((-1/8)+(0/1)*i) # Result for Rational exponent is Rational. 2 ** Rational(0, 1) # => (1/1) 2 ** Rational(1, 1) # => (2/1) 2 ** Rational(2, 1) # => (4/1) 2 ** Rational(3, 1) # => (8/1) -2 ** Rational(3, 1) # => (-8/1) 2 ** Rational(-3, 1) # => (1/8) -2 ** Rational(-3, 1) # => (-1/8)
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1294
def +: (BigDecimal) -> BigDecimal
| ...
Performs addition:
2 + 2 # => 4 -2 + 2 # => 0 -2 + -2 # => -4 2 + 2.0 # => 4.0 2 + Rational(2, 1) # => (4/1) 2 + Complex(2, 0) # => (4+0i)
Returns the sum of self and other:
1 + 1 # => 2 1 + -1 # => 0 1 + 0 # => 1 1 + -2 # => -1 1 + Complex(1, 0) # => (2+0i) 1 + Rational(1, 1) # => (2/1)
For a computation involving Floats, the result may be inexact (see Float#+):
1 + 3.14 # => 4.140000000000001
Returns the sum of self and other:
1 + 1 # => 2 1 + -1 # => 0 1 + 0 # => 1 1 + -2 # => -1 1 + Complex(1, 0) # => (2+0i) 1 + Rational(1, 1) # => (2/1)
For a computation involving Floats, the result may be inexact (see Float#+):
1 + 3.14 # => 4.140000000000001
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1310
def -: (BigDecimal) -> BigDecimal
| ...
Performs subtraction:
4 - 2 # => 2 -4 - 2 # => -6 -4 - -2 # => -2 4 - 2.0 # => 2.0 4 - Rational(2, 1) # => (2/1) 4 - Complex(2, 0) # => (2+0i)
Returns the difference of self and other:
4 - 2 # => 2 -4 - 2 # => -6 -4 - -2 # => -2 4 - 2.0 # => 2.0 4 - Rational(2, 1) # => (2/1) 4 - Complex(2, 0) # => (2+0i)
Returns the difference of self and other:
4 - 2 # => 2 -4 - 2 # => -6 -4 - -2 # => -2 4 - 2.0 # => 2.0 4 - Rational(2, 1) # => (2/1) 4 - Complex(2, 0) # => (2+0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 314
def -@: () -> Integer
Returns self, negated:
-1 # => -1 -(-1) # => 1 -0 # => 0
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1262
def /: (BigDecimal) -> BigDecimal
| ...
Performs division; for integer numeric, truncates the result to an integer:
4 / 3 # => 1 4 / -3 # => -2 -4 / 3 # => -2 -4 / -3 # => 1 For other +numeric+, returns non-integer result: 4 / 3.0 # => 1.3333333333333333 4 / Rational(3, 1) # => (4/3) 4 / Complex(3, 0) # => ((4/3)+0i)
Returns the quotient of self and other.
For integer other, truncates the result to an integer:
4 / 3 # => 1 4 / -3 # => -2 -4 / 3 # => -2 -4 / -3 # => 1
For non-integer other, returns a non-integer result:
4 / 3.0 # => 1.3333333333333333 4 / Rational(3, 1) # => (4/3) 4 / Complex(3, 0) # => ((4/3)+0i)
Returns the quotient of self and other.
For integer other, truncates the result to an integer:
4 / 3 # => 1 4 / -3 # => -2 -4 / 3 # => -2 -4 / -3 # => 1
For non-integer other, returns a non-integer result:
4 / 3.0 # => 1.3333333333333333 4 / Rational(3, 1) # => (4/3) 4 / Complex(3, 0) # => ((4/3)+0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 353
def <: (Numeric) -> bool
Returns whether the value of self is less than the value of other; other must be numeric, but may not be Complex:
1 < 0 # => false 1 < 1 # => false 1 < 2 # => true 1 < 0.5 # => false 1 < Rational(1, 2) # => false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 370
def <<: (int) -> Integer
Returns self with bits shifted count positions to the left, or to the right if count is negative:
n = 0b11110000 "%08b" % (n << 1) # => "111100000" "%08b" % (n << 3) # => "11110000000" "%08b" % (n << -1) # => "01111000" "%08b" % (n << -3) # => "00011110"
Related: Integer#>>.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 387
def <=: (Numeric) -> bool
Returns whether the value of self is less than or equal to the value of other; other must be numeric, but may not be Complex:
1 <= 0 # => false 1 <= 1 # => true 1 <= 2 # => true 1 <= 0.5 # => false 1 <= Rational(1, 2) # => false Raises an exception if the comparison cannot be made.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 415
def <=>: (Integer | Rational) -> Integer
| (untyped) -> Integer?
Compares self and other.
Returns:
-
-1, ifselfis less thanother. -
0, ifselfis equal toother. -
1, ifselfis greater thenother. -
nil, ifselfandotherare incomparable.
Examples:
1 <=> 2 # => -1 1 <=> 1 # => 0 1 <=> 1.0 # => 0 1 <=> Rational(1, 1) # => 0 1 <=> Complex(1, 0) # => 0 1 <=> 0 # => 1 1 <=> 'foo' # => nil
Class Integer includes module Comparable, each of whose methods uses Integer#<=> for comparison.
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 426
def ==: (untyped other) -> bool
Returns true if self is numerically equal to other; false otherwise.
1 == 2 #=> false 1 == 1.0 #=> true
Related: Integer#eql? (requires other to be an Integer).
(untyped other) → bool
Returns true if self is numerically equal to other; false otherwise.
1 == 2 #=> false 1 == 1.0 #=> true
Related: Integer#eql? (requires other to be an Integer).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 455
def >: (Numeric) -> bool
Returns true if the value of self is greater than that of other:
1 > 0 # => true 1 > 1 # => false 1 > 2 # => false 1 > 0.5 # => true 1 > Rational(1, 2) # => true Raises an exception if the comparison cannot be made.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 472
def >=: (Numeric) -> bool
Returns true if the value of self is greater than or equal to that of other:
1 >= 0 # => true 1 >= 1 # => true 1 >= 2 # => false 1 >= 0.5 # => true 1 >= Rational(1, 2) # => true
Raises an exception if the comparison cannot be made.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 489
def >>: (int) -> Integer
Returns self with bits shifted count positions to the right, or to the left if count is negative:
n = 0b11110000 "%08b" % (n >> 1) # => "01111000" "%08b" % (n >> 3) # => "00011110" "%08b" % (n >> -1) # => "111100000" "%08b" % (n >> -3) # => "11110000000"
Related: Integer#<<.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 530
def []: (int) -> Integer
| (int i, int len) -> Integer
| (Range[int]) -> Integer
Returns a slice of bits from self.
With argument offset, returns the bit at the given offset, where offset 0 refers to the least significant bit:
n = 0b10 # => 2 n[0] # => 0 n[1] # => 1 n[2] # => 0 n[3] # => 0
In principle, n[i] is equivalent to (n >> i) & 1. Thus, negative index always returns zero:
255[-1] # => 0
With arguments offset and size, returns size bits from self, beginning at offset and including bits of greater significance:
n = 0b111000 # => 56 "%010b" % n[0, 10] # => "0000111000" "%010b" % n[4, 10] # => "0000000011"
With argument range, returns range.size bits from self, beginning at range.begin and including bits of greater significance:
n = 0b111000 # => 56 "%010b" % n[0..9] # => "0000111000" "%010b" % n[4..9] # => "0000000011"
Raises an exception if the slice cannot be constructed.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 547
def ^: (Integer) -> Integer
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1359
def |: (Integer) -> Integer
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1373
def ~: () -> Integer
One’s complement: returns the value of self with each bit inverted.
Because an integer value is conceptually of infinite length, the result acts as if it had an infinite number of one bits to the left. In hex representations, this is displayed as two periods to the left of the digits:
sprintf("%X", ~0x1122334455) # => "..FEEDDCCBBAA"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 559
def abs: () -> Integer
Returns the absolute value of self.
(-12345).abs # => 12345 -12345.abs # => 12345 12345.abs # => 12345
(int mask) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 582
def allbits?: (int mask) -> bool
Returns true if all bits that are set (=1) in mask are also set in self; returns false otherwise.
Example values:
0b1010101 self
0b1010100 mask
0b1010100 self & mask
true self.allbits?(mask)
0b1010100 self
0b1010101 mask
0b1010100 self & mask
false self.allbits?(mask)
Related: Integer#anybits?, Integer#nobits?.
(int mask) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 605
def anybits?: (int mask) -> bool
Returns true if any bit that is set (=1) in mask is also set in self; returns false otherwise.
Example values:
0b10000010 self
0b11111111 mask
0b10000010 self & mask
true self.anybits?(mask)
0b00000000 self
0b11111111 mask
0b00000000 self & mask
false self.anybits?(mask)
Related: Integer#allbits?, Integer#nobits?.
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 650
def bit_length: () -> Integer
Returns the number of bits of the value of self, which is the bit position of the highest-order bit that is different from the sign bit (where the least significant bit has bit position 1). If there is no such bit (zero or minus one), returns zero.
This method returns ceil(log2(self < 0 ? -self : self + 1))>.
(-2**1000-1).bit_length # => 1001 (-2**1000).bit_length # => 1000 (-2**1000+1).bit_length # => 1000 (-2**12-1).bit_length # => 13 (-2**12).bit_length # => 12 (-2**12+1).bit_length # => 12 -0x101.bit_length # => 9 -0x100.bit_length # => 8 -0xff.bit_length # => 8 -2.bit_length # => 1 -1.bit_length # => 0 0.bit_length # => 0 1.bit_length # => 1 0xff.bit_length # => 8 0x100.bit_length # => 9 (2**12-1).bit_length # => 12 (2**12).bit_length # => 13 (2**12+1).bit_length # => 13 (2**1000-1).bit_length # => 1000 (2**1000).bit_length # => 1001 (2**1000+1).bit_length # => 1001
For Integer n, this method can be used to detect overflow in Array#pack:
if n.bit_length < 32 [n].pack('l') # No overflow. else raise 'Overflow' end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 692
def ceil: (?int digits) -> Integer
Returns an integer that is a “ceiling” value for self, as specified by the given ndigits, which must be an integer-convertible object. * When self is zero, returns zero (regardless of the value of ndigits): 0.ceil(2) # => 0 0.ceil(-2) # => 0
-
When
selfis non-zero andndigitsis non-negative, returnsself: 555.ceil # => 555 555.ceil(50) # => 555 -
When
selfis non-zero andndigitsis negative, returns a value based on a computed granularity:-
The granularity is
10 ** ndigits.abs. -
The returned value is the smallest multiple of the granularity that is greater than or equal to
self. Examples with positiveself: ndigits|Granularity|1234.ceil(ndigits) -------|-----------|------------------ -1| 10| 1240 -2| 100| 1300 -3| 1000| 2000 -4| 10000| 10000 -5| 100000| 100000 Examples with negativeself: ndigits|Granularity|-1234.ceil(ndigits) -------|-----------|------------------- -1| 10| -1230 -2| 100| -1200 -3| 1000| -1000 -4| 10000| 0 -5| 100000| 0 Related:Integer#floor.
-
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 710
def ceildiv: (Numeric other) -> Integer
Returns the result of division self by numeric. rounded up to the nearest integer.
3.ceildiv(3) # => 1 4.ceildiv(3) # => 2 4.ceildiv(-3) # => -1 -4.ceildiv(3) # => -1 -4.ceildiv(-3) # => 2 3.ceildiv(1.2) # => 3
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 730
def chr: (?encoding encoding) -> String
Returns a 1-character string containing the character represented by the value of self, according to the given encoding.
65.chr # => "A" 0.chr # => "\x00" 255.chr # => "\xFF" string = 255.chr(Encoding::UTF_8) string.encoding # => Encoding::UTF_8
Raises an exception if self is negative.
Related: Integer#ord.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 745
def coerce: (Numeric) -> [ Numeric, Numeric ]
() → 1
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 753
def denominator: () -> 1
Returns 1.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 768
def digits: (?int base) -> Array[Integer]
Returns an array of integers representing the base-radix digits of self; the first element of the array represents the least significant digit:
12345.digits # => [5, 4, 3, 2, 1] 12345.digits(7) # => [4, 6, 6, 0, 5] 12345.digits(100) # => [45, 23, 1]
Raises an exception if self is negative or base is less than 2.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 786
def div: (Numeric) -> Integer
Performs integer division; returns the integer result of dividing self by numeric:
4.div(3) # => 1 4.div(-3) # => -2 -4.div(3) # => -2 -4.div(-3) # => 1 4.div(3.0) # => 1 4.div(Rational(3, 1)) # => 1
Raises an exception if numeric does not have method div.
(Integer) → [ Integer, Integer ]
(Float) → [ Integer, Float ]
(Rational) → [ Integer, Rational ]
(Numeric) → [ Numeric, Numeric ]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 812
def divmod: (Integer) -> [ Integer, Integer ]
| (Float) -> [ Integer, Float ]
| (Rational) -> [ Integer, Rational ]
| (Numeric) -> [ Numeric, Numeric ]
Returns a 2-element array [q, r], where
q = (self/other).floor # Quotient r = self % other # Remainder
Examples:
11.divmod(4) # => [2, 3] 11.divmod(-4) # => [-3, -1] -11.divmod(4) # => [-3, 1] -11.divmod(-4) # => [2, -3] 12.divmod(4) # => [3, 0] 12.divmod(-4) # => [-3, 0] -12.divmod(4) # => [-3, 0] -12.divmod(-4) # => [3, 0] 13.divmod(4.0) # => [3, 1.0] 13.divmod(Rational(4, 1)) # => [3, (1/1)]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 835
def downto: (Numeric limit) { (Integer) -> void } -> Integer
| (Numeric limit) -> ::Enumerator[Integer, self]
Calls the given block with each integer value from self down to limit; returns self:
a = [] 10.downto(5) {|i| a << i } # => 10 a # => [10, 9, 8, 7, 6, 5] a = [] 0.downto(-5) {|i| a << i } # => 0 a # => [0, -1, -2, -3, -4, -5] 4.downto(5) {|i| fail 'Cannot happen' } # => 4
With no block given, returns an Enumerator.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 844
def even?: () -> bool
Returns true if self is an even number, false otherwise.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 860
def fdiv: (Numeric) -> Float
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 902
def floor: (?int digits) -> Integer
Returns an integer that is a “floor” value for self, as specified by the given ndigits, which must be an integer-convertible object. * When self is zero, returns zero (regardless of the value of ndigits): 0.floor(2) # => 0 0.floor(-2) # => 0
-
When
selfis non-zero andndigitsis non-negative, returnsself: 555.floor # => 555 555.floor(50) # => 555 -
When
selfis non-zero andndigitsis negative, returns a value based on a computed granularity:-
The granularity is
10 ** ndigits.abs. -
The returned value is the largest multiple of the granularity that is less than or equal to
self. Examples with positiveself: ndigits|Granularity|1234.floor(ndigits) -------|-----------|------------------- -1| 10| 1230 -2| 100| 1200 -3| 1000| 1000 -4| 10000| 0 -5| 100000| 0 Examples with negativeself: ndigits|Granularity|-1234.floor(ndigits) -------|-----------|-------------------- -1| 10| -1240 -2| 100| -1300 -3| 1000| -2000 -4| 10000| -10000 -5| 100000| -100000 Related:Integer#ceil.
-
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 916
def gcd: (Integer other_int) -> Integer
Returns the greatest common divisor of the two integers. The result is always positive. 0.gcd(x) and x.gcd(0) return x.abs.
36.gcd(60) #=> 12 2.gcd(2) #=> 2 3.gcd(-7) #=> 1 ((1<<31)-1).gcd((1<<61)-1) #=> 1
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 930
def gcdlcm: (Integer other_int) -> [ Integer, Integer ]
Returns an array with the greatest common divisor and the least common multiple of the two integers, [gcd, lcm].
36.gcdlcm(60) #=> [12, 180] 2.gcdlcm(2) #=> [2, 2] 3.gcdlcm(-7) #=> [1, 21] ((1<<31)-1).gcdlcm((1<<61)-1) #=> [1, 4951760154835678088235319297]
Returns a string containing the place-value representation of self in radix base (in 2..36).
12345.to_s # => "12345" 12345.to_s(2) # => "11000000111001" 12345.to_s(8) # => "30071" 12345.to_s(10) # => "12345" 12345.to_s(16) # => "3039" 12345.to_s(36) # => "9ix" 78546939656932.to_s(36) # => "rubyrules"
Raises an exception if base is out of range.
() → true
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 954
def integer?: () -> true
Since self is already an Integer, always returns true.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 968
def lcm: (Integer other_int) -> Integer
Returns the least common multiple of the two integers. The result is always positive. 0.lcm(x) and x.lcm(0) return zero.
36.lcm(60) #=> 180 2.lcm(2) #=> 2 3.lcm(-7) #=> 21 ((1<<31)-1).lcm((1<<61)-1) #=> 4951760154835678088235319297
Returns self modulo other as a real numeric (Integer, Float, or Rational).
For integer n and real number r, these expressions are equivalent:
n % r n-r*(n/r).floor n.divmod(r)[1]
See Numeric#divmod.
Examples:
10 % 2 # => 0 10 % 3 # => 1 10 % 4 # => 2 10 % -2 # => 0 10 % -3 # => -2 10 % -4 # => -2 10 % 3.0 # => 1.0 10 % Rational(3, 1) # => (1/1)
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1003
def negative?: () -> bool
Returns the successor integer of self (equivalent to self + 1):
1.succ #=> 2 -1.succ #=> 0
Related: Integer#pred (predecessor value).
(int mask) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1036
def nobits?: (int mask) -> bool
Returns true if no bit that is set (=1) in mask is also set in self; returns false otherwise.
Example values:
0b11110000 self
0b00001111 mask
0b00000000 self & mask
true self.nobits?(mask)
0b00000001 self
0b11111111 mask
0b00000001 self & mask
false self.nobits?(mask)
Related: Integer#allbits?, Integer#anybits?.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1044
def numerator: () -> self
Returns self.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1052
def odd?: () -> bool
Returns true if self is an odd number, false otherwise.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1060
def ord: () -> self
Returns self; intended for compatibility to character literals in Ruby 1.9.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1062
def polar: () -> [ Integer, Integer | Float ]
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1064
def positive?: () -> bool
(Integer other) → (Integer | Rational)
(Integer other, Integer modulo) → Integer
(Float) → (Float | Complex)
(Rational) → (Float | Rational | Complex)
(Complex) → Complex
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1076
def pow: (Integer other) -> (Integer | Rational)
| (Integer other, Integer modulo) -> Integer
| (Float) -> (Float | Complex)
| (Rational) -> (Float | Rational | Complex)
| (Complex) -> Complex
Returns (modular) exponentiation as:
a.pow(b) #=> same as a**b a.pow(b, m) #=> same as (a**b) % m, but avoids huge temporary values
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1093
def pred: () -> Integer
Returns the predecessor of self (equivalent to self - 1):
1.pred #=> 0 -1.pred #=> -2
Related: Integer#succ (successor value).
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1095
def quo: (Integer) -> Rational
| (Float) -> Float
| (Rational) -> Rational
| (Complex) -> Complex
| (Numeric) -> Numeric
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1108
def rationalize: (?Numeric eps) -> Rational
Returns the value as a rational. The optional argument eps is always ignored.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1110
def rect: () -> [ Integer, Numeric ]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1133
def remainder: (Integer) -> Integer
| (Float) -> Float
| (Rational) -> Rational
| (Numeric) -> Numeric
Returns the remainder after dividing self by other.
Examples:
11.remainder(4) # => 3 11.remainder(-4) # => 3 -11.remainder(4) # => -3 -11.remainder(-4) # => -3 12.remainder(4) # => 0 12.remainder(-4) # => 0 -12.remainder(4) # => 0 -12.remainder(-4) # => 0 13.remainder(4.0) # => 1.0 13.remainder(Rational(4, 1)) # => (1/1)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1184
def round: (?int digits, ?half: Numeric::round_mode) -> Integer
Returns self rounded to the nearest value with a precision of ndigits decimal digits.
When ndigits is negative, the returned value has at least ndigits.abs trailing zeros:
555.round(-1) # => 560 555.round(-2) # => 600 555.round(-3) # => 1000 -555.round(-2) # => -600 555.round(-4) # => 0
Returns self when ndigits is zero or positive.
555.round # => 555 555.round(1) # => 555 555.round(50) # => 555
If keyword argument half is given, and self is equidistant from the two candidate values, the rounding is according to the given half value:
-
:upornil: round away from zero:25.round(-1, half: :up) # => 30 (-25).round(-1, half: :up) # => -30
-
:down: round toward zero:25.round(-1, half: :down) # => 20 (-25).round(-1, half: :down) # => -20
-
:even: round toward the candidate whose last nonzero digit is even:25.round(-1, half: :even) # => 20 15.round(-1, half: :even) # => 20 (-25).round(-1, half: :even) # => -20
Raises and exception if the value for half is invalid.
Related: Integer#truncate.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1200
def size: () -> Integer
Returns the number of bytes in the machine representation of self; the value is system-dependent:
1.size # => 8 -1.size # => 8 2147483647.size # => 8 (256**10 - 1).size # => 10 (256**20 - 1).size # => 20 (256**40 - 1).size # => 40
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1213
def succ: () -> Integer
Returns the successor integer of self (equivalent to self + 1):
1.succ #=> 2 -1.succ #=> 0
Related: Integer#pred (predecessor value).
() { (Integer i) → void } → self
() → Enumerator[Integer, self]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1229
def times: () { (Integer i) -> void } -> self
| () -> Enumerator[Integer, self]
Calls the given block self times with each integer in (0..self-1):
a = [] 5.times {|i| a.push(i) } # => 5 a # => [0, 1, 2, 3, 4]
With no block given, returns an Enumerator.
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/lib/bigdecimal/util.rb, line 23 def to_d BigDecimal(self) end
Returns the value of int as a BigDecimal.
require 'bigdecimal' require 'bigdecimal/util' 42.to_d # => 0.42e2
See also Kernel.BigDecimal.
Returns the value of int as a BigDecimal.
require 'bigdecimal' require 'bigdecimal/util' 42.to_d # => 0.42e2
See also Kernel.BigDecimal.
Returns the value of int as a BigDecimal.
require 'bigdecimal' require 'bigdecimal/util' 42.to_d # => 0.42e2
See also Kernel.BigDecimal.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1246
def to_f: () -> Float
Converts self to a Float:
1.to_f # => 1.0 -1.to_f # => -1.0
If the value of self does not fit in a Float, the result is infinity:
(10**400).to_f # => Infinity (-10**400).to_f # => -Infinity
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1254
def to_i: () -> self
Returns self (which is already an Integer).
(?JSON::State? state) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/stdlib/json/0/json.rbs, line 1289
def to_json: (?JSON::State? state) -> String
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1273
def to_r: () -> Rational
Returns the value as a rational.
1.to_r #=> (1/1) (1<<64).to_r #=> (18446744073709551616/1)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1292
def to_s: (?int base) -> String
Returns a string containing the place-value representation of self in radix base (in 2..36).
12345.to_s # => "12345" 12345.to_s(2) # => "11000000111001" 12345.to_s(8) # => "30071" 12345.to_s(10) # => "12345" 12345.to_s(16) # => "3039" 12345.to_s(36) # => "9ix" 78546939656932.to_s(36) # => "rubyrules"
Raises an exception if base is out of range.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1315
def truncate: (?int ndigits) -> Integer
Returns self truncated (toward zero) to a precision of ndigits decimal digits.
When ndigits is negative, the returned value has at least ndigits.abs trailing zeros:
555.truncate(-1) # => 550 555.truncate(-2) # => 500 -555.truncate(-2) # => -500
Returns self when ndigits is zero or positive.
555.truncate # => 555 555.truncate(50) # => 555
Related: Integer#round.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1335
def upto: (Numeric limit) { (Integer) -> void } -> Integer
| (Numeric limit) -> ::Enumerator[Integer, self]
Calls the given block with each integer value from self up to limit; returns self:
a = [] 5.upto(10) {|i| a << i } # => 5 a # => [5, 6, 7, 8, 9, 10] a = [] -5.upto(0) {|i| a << i } # => -5 a # => [-5, -4, -3, -2, -1, 0] 5.upto(4) {|i| fail 'Cannot happen' } # => 5
With no block given, returns an Enumerator.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/integer.rbs, line 1344
def zero?: () -> bool
Returns true if self has a zero value, false otherwise.