Class: Integer
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::Numeric
|
|
|
Instance Chain:
self,
::Numeric,
::Comparable
|
|
| Inherits: | Numeric |
| Defined in: | numeric.c, bignum.c, rational.c |
Overview
Holds Integer values. You cannot add a singleton method to an Integer. Any attempt to add a singleton method to an Integer object will raise a ::TypeError.
Constant Summary
-
GMP_VERSION =
# File 'bignum.c', line 6802
The version of loaded GMP.
rb_sprintf("GMP %s", gmp_version)
Instance Attribute Summary
-
#even? ⇒ Boolean
readonly
Returns
trueifintis an even number. -
#integer? ⇒ Boolean
readonly
Since
intis already anInteger, this always returnstrue. -
#odd? ⇒ Boolean
readonly
Returns
trueifintis an odd number.
::Numeric - Inherited
| #finite? | Return true if |
| #infinite? | Returns values corresponding to the value of |
| #integer? | Returns |
| #negative? | Returns |
| #nonzero? | Returns |
| #positive? | Returns |
| #real | Returns self. |
| #real? | Returns |
| #zero? | Returns |
Instance Method Summary
-
#%(other) ⇒ Numeric
(also: #modulo)
Returns
intmoduloother. -
#&(integer) ⇒ Integer
Bitwise AND.
-
#*(numeric) ⇒ numeric_result
Performs multiplication: the class of the resulting object depends on the class of
numericand on the magnitude of the result. -
#**(numeric) ⇒ numeric_result
Raises
integerto the power ofnumeric, which may be negative or fractional. -
#+(numeric) ⇒ numeric_result
Performs addition: the class of the resulting object depends on the class of
numericand on the magnitude of the result. -
#-(numeric) ⇒ numeric_result
Performs subtraction: the class of the resulting object depends on the class of
numericand on the magnitude of the result. -
#- ⇒ Integer
Negates
int. -
#/(numeric) ⇒ numeric_result
Performs division: the class of the resulting object depends on the class of
numericand on the magnitude of the result. -
#<(real) ⇒ Boolean
Returns
trueif the value ofintis less than that ofreal. -
#<<(count) ⇒ Integer
Shifts
intleftcountpositions, or right ifcountis negative. -
#<=(real) ⇒ Boolean
Returns
trueif the value ofintis less than or equal to that ofreal. -
#<=>(numeric) ⇒ 1, ...
Comparison—Returns
-1,0, +1ornildepending on whetherintis less than, equal to, or greater thannumeric. - #==(y) (also: #===)
-
#===(y)
Alias for #==.
-
#>(real) ⇒ Boolean
Returns
trueif the value ofintis greater than that ofreal. -
#>=(real) ⇒ Boolean
Returns
trueif the value ofintis greater than or equal to that ofreal. -
#>>(count) ⇒ Integer
Shifts
intrightcountpositions, or left ifcountis negative. -
#[](n) ⇒ 0, 1
Bit Reference—Returns the
nth bit in the binary representation ofint, whereint[0]is the least significant bit. -
#^(integer) ⇒ Integer
Bitwise EXCLUSIVE OR.
-
#abs ⇒ Integer
(also: #magnitude)
Returns the absolute value of
int. -
#bit_length ⇒ Integer
Returns the number of bits of the value of int.
-
#ceil([ndigits]) ⇒ Integer, Float
Returns the smallest number than or equal to
intin decimal digits (default 0 digits). -
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the
int's value according toencoding. -
#coerce(numeric) ⇒ Array
Returns an array with both a
numericand abigrepresented as Bignum objects. -
#denominator ⇒ 1
Returns 1.
-
#digits ⇒ Array
Returns the array including the digits extracted by place-value notation with radix
baseofint. -
#div(numeric) ⇒ Integer
Performs integer division: returns integer result of dividing
intbynumeric. -
#divmod(numeric) ⇒ Array
See Numeric#divmod.
-
#downto(limit) {|i| ... } ⇒ self
Iterates the given block, passing decreasing values from
intdown to and includinglimit. -
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing
integerbynumeric. -
#floor([ndigits]) ⇒ Integer, Float
Returns the largest number less than or equal to
intin decimal digits (default 0 digits). -
#gcd(int2) ⇒ Integer
Returns the greatest common divisor (always positive).
-
#gcdlcm(int2) ⇒ Array
Returns an array; [int.gcd(int2), int.lcm(int2)].
-
#inspect(base = 10) ⇒ String
Alias for #to_s.
-
#lcm(int2) ⇒ Integer
Returns the least common multiple (always positive).
-
#magnitude ⇒ Integer
Alias for #abs.
-
#modulo(other) ⇒ Numeric
Alias for #%.
-
#next ⇒ Integer
(also: #succ)
Returns the
Integerequal toint+ 1. -
#numerator ⇒ self
Returns self.
-
#ord ⇒ self
Returns the
intitself. -
#pred ⇒ Integer
Returns the
Integerequal toint- 1. -
#rationalize([eps]) ⇒ Rational
Returns the value as a rational.
-
#remainder(numeric) ⇒ Numeric
Returns the remainder after dividing big by numeric as:
-
#round([ndigits]) ⇒ Integer, Float
Rounds
intto a given precision in decimal digits (default 0 digits). -
#size ⇒ Integer
Returns the number of bytes in the machine representation of
int. -
#succ ⇒ Integer
Alias for #next.
-
#times {|i| ... } ⇒ self
Iterates the given block
inttimes, passing in values from zero toint - 1. -
#to_f ⇒ Float
Converts
intto a ::Float. -
#to_i ⇒ Integer
(also: #to_int)
As
intis already anInteger, all these methods simply return the receiver. -
#to_int ⇒ Integer
Alias for #to_i.
-
#to_r ⇒ Rational
Returns the value as a rational.
-
#to_s(base = 10) ⇒ String
(also: #inspect)
Returns a string containing the representation of
intradixbase(between 2 and 36). -
#truncate([ndigits]) ⇒ Integer, Float
Returns the smallest number than or equal to
intin decimal digits (default 0 digits). -
#upto(limit) {|i| ... } ⇒ self
Iterates the given block, passing in integer values from
intup to and includinglimit. -
#|(integer) ⇒ Integer
Bitwise OR.
-
#~ ⇒ Integer
One's complement: returns a number where each bit is flipped.
::Numeric - Inherited
| #% | x.modulo(y) means x-y*(x/y).floor. |
| #+@ | Unary Plus—Returns the receiver's value. |
| #-@ | Unary Minus—Returns the receiver's value, negated. |
| #<=> | Returns zero if |
| #abs | Returns the absolute value of |
| #abs2 | Returns square of self. |
| #angle | Alias for Numeric#arg. |
| #arg | Returns 0 if the value is positive, pi otherwise. |
| #ceil | Returns the smallest possible |
| #coerce, | |
| #conj | Returns self. |
| #conjugate | Alias for Numeric#conj. |
| #denominator | Returns the denominator (always positive). |
| #div | Uses #/ to perform division, then converts the result to an integer. |
| #divmod | Returns an array containing the quotient and modulus obtained by dividing |
| #eql? | Returns |
| #fdiv | Returns float division. |
| #floor | Returns the largest integer less than or equal to |
| #i | Returns the corresponding imaginary number. |
| #imag | Returns zero. |
| #imaginary | Alias for Numeric#imag. |
| #initialize_copy | Numerics are immutable values, which should not be copied. |
| #magnitude | Alias for Numeric#abs. |
| #modulo | Alias for Numeric#%. |
| #numerator | Returns the numerator. |
| #phase | Alias for Numeric#arg. |
| #polar | Returns an array; [num.abs, num.arg]. |
| #quo | Returns most exact division (rational for integers, float for floats). |
| #rect | Returns an array; [num, 0]. |
| #rectangular | Alias for Numeric#rect. |
| #remainder | x.remainder(y) means x-y*(x/y).truncate. |
| #round | Rounds |
| #singleton_method_added | Trap attempts to add methods to ::Numeric objects. |
| #step | Invokes the given block with the sequence of numbers starting at |
| #to_c | Returns the value as a complex. |
| #to_int | Invokes the child class's #to_i method to convert |
| #truncate | Returns |
::Comparable - Included
| #< | Compares two objects based on the receiver's #<=> method, returning true if it returns -1. |
| #<= | Compares two objects based on the receiver's #<=> method, returning true if it returns -1 or 0. |
| #== | Compares two objects based on the receiver's #<=> method, returning true if it returns 0. |
| #> | Compares two objects based on the receiver's #<=> method, returning true if it returns 1. |
| #>= | Compares two objects based on the receiver's #<=> method, returning true if it returns 0 or 1. |
| #between? | |
| #clamp |
Instance Attribute Details
#even? ⇒ Boolean (readonly)
Returns true if int is an even number.
#integer? ⇒ Boolean (readonly)
Since int is already an Integer, this always returns true.
#odd? ⇒ Boolean (readonly)
Returns true if int is an odd number.
Instance Method Details
Also known as: #modulo
Returns int modulo other.
See Numeric#divmod for more information.
#&(integer) ⇒ Integer
Bitwise AND.
#*(numeric) ⇒ numeric_result
Performs multiplication: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.
#**(numeric) ⇒ numeric_result
Raises integer to the power of numeric, which may be negative or fractional. The result may be an Integer, or a ::Float
2 ** 3 #=> 8
2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623731
123456789 ** 2 #=> 15241578750190521
123456789 ** 1.2 #=> 5126464716.09932
123456789 ** -2 #=> (1/15241578750190521)
#+(numeric) ⇒ numeric_result
Performs addition: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.
#-(numeric) ⇒ numeric_result
Performs subtraction: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.
#- ⇒ Integer
Negates int. (returns an integer whose value is 0-int)
#/(numeric) ⇒ numeric_result
Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.
#<(real) ⇒ Boolean
Returns true if the value of int is less than that of real.
#<<(count) ⇒ Integer
Shifts int left count positions, or right if count is negative.
#<=(real) ⇒ Boolean
Returns true if the value of int is less than or equal to that of real.
#<=>(numeric) ⇒ 1, ...
Comparison—Returns -1, 0, +1 or nil depending on whether int is less than, equal to, or greater than numeric.
This is the basis for the tests in the ::Comparable module.
nil is returned if the two values are incomparable.
#==(y) Also known as: #===
#===(y)
Alias for #==.
#>(real) ⇒ Boolean
Returns true if the value of int is greater than that of real.
#>=(real) ⇒ Boolean
Returns true if the value of int is greater than or equal to that of real.
#>>(count) ⇒ Integer
Shifts int right count positions, or left if count is negative.
#[](n) ⇒ 0, 1
Bit Reference—Returns the nth bit in the binary representation of int, where int[0] is the least significant bit.
For example:
a = 0b11001100101010
30.downto(0) do |n| print a[n] end
#=> 0000000000000000011001100101010
a = 9**15
50.downto(0) do |n|
print a[n]
end
#=> 000101110110100000111000011110010100111100010111001
#^(integer) ⇒ Integer
Bitwise EXCLUSIVE OR.
#abs ⇒ Integer
#magnitude ⇒ Integer
Also known as: #magnitude
Integer
#magnitude ⇒ Integer
Returns the absolute value of int.
-12345.abs #=> 12345
12345.abs #=> 12345
-1234567890987654321.abs #=> 1234567890987654321
#bit_length ⇒ Integer
Returns the number of bits of the value of int.
“the number of bits” means that the bit position of the highest bit which is different to the sign bit. (The bit position of the bit 2**n is n+1.) If there is no such bit (zero or minus one), zero is returned.
I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
(-2**10000-1).bit_length #=> 10001
(-2**10000).bit_length #=> 10000
(-2**10000+1).bit_length #=> 10000
(-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
(2**10000-1).bit_length #=> 10000
(2**10000).bit_length #=> 10001
(2**10000+1).bit_length #=> 10001
This method can be used to detect overflow in Array#pack as follows.
if n.bit_length < 32
[n].pack("l") # no overflow
else
raise "overflow"
end
#ceil([ndigits]) ⇒ Integer, Float
Returns the smallest number than or equal to int in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits is positive, self for zero, and ceil up for negative.
1.ceil #=> 1
1.ceil(2) #=> 1.0
15.ceil(-1) #=> 20
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the int's value according to encoding.
65.chr #=> "A"
230.chr #=> "\346"
255.chr(Encoding::UTF_8) #=> "\303\277"
#coerce(numeric) ⇒ Array
Returns an array with both a numeric and a big represented as Bignum objects.
This is achieved by converting numeric to a Bignum.
A TypeError is raised if the numeric is not a Fixnum or Bignum type.
(0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
#denominator ⇒ 1
Returns 1.
Returns the array including the digits extracted by place-value notation with radix base of int.
base should be greater than or equal to 2.
12345.digits #=> [5, 4, 3, 2, 1]
12345.digits(7) #=> [4, 6, 6, 0, 5]
12345.digits(100) #=> [45, 23, 1]
-12345.digits(7) #=> Math::DomainError
#div(numeric) ⇒ Integer
Performs integer division: returns integer result of dividing int by numeric.
#divmod(numeric) ⇒ Array
See Numeric#divmod.
#downto(limit) {|i| ... } ⇒ self
#downto(limit) ⇒ Enumerator
self
#downto(limit) ⇒ Enumerator
Iterates the given block, passing decreasing values from int down to and including limit.
If no block is given, an ::Enumerator is returned instead.
5.downto(1) { |n| print n, ".. " }
print " Liftoff!\n"
#=> "5.. 4.. 3.. 2.. 1.. Liftoff!"
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing integer by numeric.
654321.fdiv(13731) #=> 47.6528293642124
654321.fdiv(13731.24) #=> 47.6519964693647
-1234567890987654321.fdiv(13731) #=> -89910996357705.5
-1234567890987654321.fdiv(13731.24) #=> -89909424858035.7
#floor([ndigits]) ⇒ Integer, Float
Returns the largest number less than or equal to int in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits is positive, self for zero, and floor down for negative.
1.floor #=> 1
1.floor(2) #=> 1.0
15.floor(-1) #=> 10
#gcd(int2) ⇒ Integer
Returns the greatest common divisor (always positive). 0.gcd(x) and x.gcd(0) return abs(x).
2.gcd(2) #=> 2
3.gcd(-7) #=> 1
((1<<31)-1).gcd((1<<61)-1) #=> 1
#gcdlcm(int2) ⇒ Array
Returns an array; [int.gcd(int2), int.lcm(int2)].
2.gcdlcm(2) #=> [2, 2]
3.gcdlcm(-7) #=> [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1) #=> [1, 4951760154835678088235319297]
Alias for #to_s.
#lcm(int2) ⇒ Integer
Returns the least common multiple (always positive). 0.lcm(x) and x.lcm(0) return zero.
2.lcm(2) #=> 2
3.lcm(-7) #=> 21
((1<<31)-1).lcm((1<<61)-1) #=> 4951760154835678088235319297
#abs ⇒ Integer
#magnitude ⇒ Integer
Integer
#magnitude ⇒ Integer
Alias for #abs.
Alias for #%.
#next ⇒ Integer
#succ ⇒ Integer
Also known as: #succ
Integer
#succ ⇒ Integer
#numerator ⇒ self
Returns self.
#ord ⇒ self
Returns the int itself.
?a.ord #=> 97
This method is intended for compatibility to character constant in Ruby 1.9.
For example, ?a.ord returns 97 both in 1.8 and 1.9.
#pred ⇒ Integer
Returns the Integer equal to int - 1.
1.pred #=> 0
(-1).pred #=> -2
#rationalize([eps]) ⇒ Rational
Returns the value as a rational. The optional argument eps is always ignored.
#remainder(numeric) ⇒ Numeric
Returns the remainder after dividing big by numeric as:
x.remainder(y) means x-y*(x/y).truncate
Examples
5.remainder(3) #=> 2
-5.remainder(3) #=> -2
5.remainder(-3) #=> 2
-5.remainder(-3) #=> -2
-1234567890987654321.remainder(13731) #=> -6966
-1234567890987654321.remainder(13731.24) #=> -9906.22531493148
See Numeric#divmod.
#round([ndigits]) ⇒ Integer, Float
Rounds int to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits is positive, self for zero, and round down for negative.
1.round #=> 1
1.round(2) #=> 1.0
15.round(-1) #=> 20
#size ⇒ Integer
Returns the number of bytes in the machine representation of int.
1.size #=> 4
-1.size #=> 4
2147483647.size #=> 4
(256**10 - 1).size #=> 12
(256**20 - 1).size #=> 20
(256**40 - 1).size #=> 40
#next ⇒ Integer
#succ ⇒ Integer
Integer
#succ ⇒ Integer
Alias for #next.
#times {|i| ... } ⇒ self
#times ⇒ Enumerator
self
#times ⇒ Enumerator
Iterates the given block int times, passing in values from zero to int - 1.
If no block is given, an ::Enumerator is returned instead.
5.times do |i|
print i, " "
end
#=> 0 1 2 3 4
#to_f ⇒ Float
#to_i ⇒ Integer Also known as: #to_int
As int is already an Integer, all these methods simply return the receiver.
Synonyms is #to_int
#to_i ⇒ Integer
#to_int ⇒ Integer
Integer
#to_int ⇒ Integer
Alias for #to_i.
#to_r ⇒ Rational
Returns the value as a rational.
1.to_r #=> (1/1)
(1<<64).to_r #=> (18446744073709551616/1)
#to_s(base = 10) ⇒ String Also known as: #inspect
Returns a string containing the representation of int radix base (between 2 and 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"
#truncate([ndigits]) ⇒ Integer, Float
Returns the smallest number than or equal to int in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits is positive, self for zero, and truncate up for negative.
1.truncate #=> 1
1.truncate(2) #=> 1.0
15.truncate(-1) #=> 10
#upto(limit) {|i| ... } ⇒ self
#upto(limit) ⇒ Enumerator
self
#upto(limit) ⇒ Enumerator
Iterates the given block, passing in integer values from int up to and including limit.
If no block is given, an ::Enumerator is returned instead.
For example:
5.upto(10) { |i| print i, " " }
#=> 5 6 7 8 9 10
#|(integer) ⇒ Integer
Bitwise OR.
#~ ⇒ Integer
One's complement: returns a number where each bit is flipped.
Inverts the bits in an integer. As Integers are conceptually 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"