Integer
26pages on
this wiki
Add New Page
this wiki
The class Integer is the base class of the classes Fixnum
and Bignum
. Fixnum
is the class of all integers within a certain machinedependent 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.
Method  Parameters  Description 

==  1  Returns true if self is equal to the parameter.

>  1  Returns true if self is greater than the parameter.

>=  1  Returns true if self is greater than or equal to the parameter.

<  1  Returns true if self is less than the parameter.

<=  1  Returns 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.

@  0  Unary minus. Returns the negation of self .

+@  0  Unary plus. Returns self unchanged.

+  1  Addition operator. Returns the sum of self and the single parameter.

  1  Subtraction operator. Returns the difference of self and the single parameter.

*  1  Multiplication operator. Returns the product of self and the single parameter.

/  1  Division operator. Returns the integer quotient, rounded downward, of self and the single parameter.

%  1  Modulo 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.

**  1  Power operator. Returns self raised to the power of the single parameter.

~  0  Binary complement operator. Returns the bitinversion of self .

&  1  Binary AND operator. Returns the result of binary AND of self and the single parameter.

  1  Binary OR operator. Returns the result of binary OR of self and the single parameter.

^  1  Binary XOR operator. Returns the result of binary XOR of self and the single parameter.

[]  1  Bit 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 .

<<  1  Left shift operator. Returns self , shifted left by the single parameter. A negative left shift is a shift to the right.

>>  1  Right shift operator. Returns self , shifted right by the single parameter. A negative right shift is a shift to the left.

abs  0  Returns the absolute value of self .

between?  2  self.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 .

ceil  0  Returns self unchanged.

chr  0  Returns a String with a single character whose byte value is self . self must be between 0 and 255 inclusive.

coerce  1  self.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 .

div  1  self.div(other) is equivalent to (self/other).floor .

divmod  1  self.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.

downto  1  self.downto(limit) calls a passed block, passing it self and then self1 , and so on until the limit is exceeded. Equivalent to self.step(limit, 1) .

eql?  1  self.eql?(other) returns true if self and other have the same type and equal values.

floor  0  Returns self unchanged.

hash  0  Returns a hash for use by Hash objects.

id2name  0  Defined for Fixnum only. Returns the name of the object whose symbol ID is self . If no such object exists, returns nil .

induced_from  1  Singleton 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?  0  Returns true .

modulo  1  self.modulo(other) is equivalent to self % other .

nonzero?  0  Returns true if self is not equal to zero.

next  0  Returns self+1 .

prec  1  self.prec(klass) converts self to an object of class klass , using the induced_from singleton method of that class.

prec_f  1  self.prec_f is equivalent to self.prec(Float) .

prec_i  1  self.prec_i is equivalent to self.prec(Integer) .

quo  1  self.quo(other) returns the result of self divided by other as a Float .

remainder  1  self .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.

round  0  Returns self unchanged.

size  0  For 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.

succ  0  Synonym for next .

step  1 or 2  self.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.

times  0  Calls a passed block, passing it 0, 1, 2, and so on until self is reached. Equivalent to 0.step(self1, +1) .

to_f  0  Returns self converted to a Float .

to_i  0  Returns self unchanged.

to_int  0  Returns self unchanged.

to_s  0  Returns a string representation of self .

to_sym  0  Defined for Fixnum only. Returns the symbol whose integer value is self . If no such symbol exists, returns nil .

truncate  0  Returns self unchanged.

upto  1  self.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?  0  Returns true if self is equal to zero.

New in Ruby 1.9Edit
Method  Parameters  Description 

chr  0 or 1  With 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 UTF8 encoding supports the original ISO 106461 encoding with code points from 0 to 2^{31}1.

div  1  self.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?  0  Returns true if self is an even integer.

fdiv  1  A synonym for quo .

odd?  0  Returns true if self is an odd integer.

pred  0  Returns self1 .

round  0 or 1  With 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?  0  Returns true .
