FANDOM


The class Integer is the base class of the classes Fixnum and Bignum. Fixnum is the class of all integers within a certain machine-dependent range that can be implemented more efficiently than Bignum can. Bignum defines signed integers with range limited only by available memory.

Arithmetic involving only Bignum and Fixnum will produce a Fixnum if the result is small enough, and Bignum otherwise. The two types are thus more or less interchangeable and the programmer will seldom if ever need to worry about the difference between them.

Inherits: Numeric.

Mixins: Comparable (from Numeric), Precision.

MethodsEdit

Except as noted, all methods are available for both Fixnum and Bignum objects.

MethodParametersDescription
==1Returns true if self is equal to the parameter.
>1Returns true if self is greater than the parameter.
>=1Returns true if self is greater than or equal to the parameter.
<1Returns true if self is less than the parameter.
<=1Returns true if self is less than or equal to the parameter.
<=>1"Spaceship" operator. Returns -1 if self is less than the parameter, 0 if equal, +1 if greater.
-@0Unary minus. Returns the negation of self.
+@0Unary plus. Returns self unchanged.
+1Addition operator. Returns the sum of self and the single parameter.
-1Subtraction operator. Returns the difference of self and the single parameter.
*1Multiplication operator. Returns the product of self and the single parameter.
/1Division operator. Returns the integer quotient, rounded downward, of self and the single parameter.
%1Modulo operator. Returns the remainder from dividing self by the single paramter. The remainder is determined by rounding the quotient downward and has the same sign as the modulus.
**1Power operator. Returns self raised to the power of the single parameter.
~0Binary complement operator. Returns the bit-inversion of self.
&1Binary AND operator. Returns the result of binary AND of self and the single parameter.
|1Binary OR operator. Returns the result of binary OR of self and the single parameter.
^1Binary XOR operator. Returns the result of binary XOR of self and the single parameter.
[]1Bit index operator. self[other] returns the other'th bit of self, where the least significant bit is zero and the more significant bits count to the left. self[other] is equivalent to (self>>other) & 1.
<<1Left shift operator. Returns self, shifted left by the single parameter. A negative left shift is a shift to the right.
>>1Right shift operator. Returns self, shifted right by the single parameter. A negative right shift is a shift to the left.
abs0Returns the absolute value of self.
between?2self.between(min, max) returns true if self is not less than min and not greater than max. The limits are part of the range, so 2.between?(2, 6) returns true.
ceil0Returns self unchanged.
chr0Returns a String with a single character whose byte value is self. self must be between 0 and 255 inclusive.
coerce1self.coerce(other) returns an Array representing other and self in that order. If self and other have the same type, they are returned unchanged; otherwise, both are converted to Float.
div1self.div(other) is equivalent to (self/other).floor.
divmod1self.divmod(other) returns an Array containing the integer quotient and the remainder, in that order, resulting from dividing self by other. The quotient is an Integer and is rounded downward; the remainder is either an Integer or a Float and satisfies (quo*other+rem) == self. This is equivalent to [self.div(other), self.modulo(other)], except that self and other are evaluated only once.
downto1self.downto(limit) calls a passed block, passing it self and then self-1, and so on until the limit is exceeded. Equivalent to self.step(limit, -1).
eql?1self.eql?(other) returns true if self and other have the same type and equal values.
floor0Returns self unchanged.
hash0Returns a hash for use by Hash objects.
id2name0Defined for Fixnum only. Returns the name of the object whose symbol ID is self. If no such object exists, returns nil.
induced_from1Singleton method. The single parameter is of Numeric or derived type. For all classes that define it, class::induced_from returns the parameter converted to that class.
integer?0Returns true.
modulo1self.modulo(other) is equivalent to self % other.
nonzero?0Returns true if self is not equal to zero.
next0Returns self+1.
prec1self.prec(klass) converts self to an object of class klass, using the induced_from singleton method of that class.
prec_f1self.prec_f is equivalent to self.prec(Float).
prec_i1self.prec_i is equivalent to self.prec(Integer).
quo1self.quo(other) returns the result of self divided by other as a Float.
remainder1self.remainder(other) returns the remainder that results from dividing self by other. Different from modulo in that the remainder is derived by rounding the quotient toward zero rather than downward and has the same sign as self; the sign of other is ignored.
round0Returns self unchanged.
size0For Fixnum, returns the number of bytes in the machine word used to represent a Fixnum. For Bignum, returns the size of a Bignum "digit" times the number of digits.
succ0Synonym for next.
step1 or 2self.step(limit, stride) calls a passed block, passing it self the first time, and then self+stride, self+2*stride, and so on until the limit is exceeded. A step value that is equal to the limit will call the block one last time. If stride is not present, it is taken to be 1.
times0Calls a passed block, passing it 0, 1, 2, and so on until self is reached. Equivalent to 0.step(self-1, +1).
to_f0Returns self converted to a Float.
to_i0Returns self unchanged.
to_int0Returns self unchanged.
to_s0Returns a string representation of self.
to_sym0Defined for Fixnum only. Returns the symbol whose integer value is self. If no such symbol exists, returns nil.
truncate0Returns self unchanged.
upto1self.upto(limit) calls a passed block, passing it self and then self+1, and so on until the limit is exceeded. Equivalent to self.step(limit, +1).
zero?0Returns true if self is equal to zero.

New in Ruby 1.9Edit

MethodParametersDescription
chr0 or 1With no parameter, chr behaves as in Ruby 1.8. The optional parameter is a String specifying a character encoding. self must be a valid code point in that encoding; usually that is 0 to 255, but the UTF-8 encoding supports the original ISO 10646-1 encoding with code points from 0 to 231-1.
div1self.div(other) is no longer equivalent to (self/other).floor, but to (self/other).to_i. That is, when other is a float, Ruby 1.9 rounds toward zero, where Ruby 1.8 rounds down.
even?0Returns true if self is an even integer.
fdiv1A synonym for quo.
odd?0Returns true if self is an odd integer.
pred0Returns self-1.
round0 or 1With no parameter, round behaves as in Ruby 1.8. An optional numeric parameter gives the number of digits to the right of the decimal point to which self is to be rounded. This number may be negative, in which case self will be rounded to a power of ten equal to the negation of the digit count. If the number of digits is positive, the return value is a Float; otherwise it is an Integer.
scalar?0Returns true.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.