In Files

  • bignum.c
  • numeric.c
  • rational.c

Class/Module Index [+]

Quicksearch

Integer

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.

Constants

GMP_VERSION

The version of loaded GMP.

Public Instance Methods

int % other → real click to toggle source

Returns int modulo other.

See Numeric#divmod for more information.

 
               VALUE
rb_int_modulo(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_mod(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_modulo(x, y);
    }
    return num_modulo(x, y);
}
            
integer & integer → integer_result click to toggle source

Bitwise AND.

 
               VALUE
rb_int_and(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_and(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_and(x, y);
    }
    return Qnil;
}
            
int * numeric → numeric_result click to toggle source

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.

 
               VALUE
rb_int_mul(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_mul(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_mul(x, y);
    }
    return rb_num_coerce_bin(x, y, '*');
}
            
integer ** numeric → numeric_result click to toggle source

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)
 
               VALUE
rb_int_pow(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_pow(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_pow(x, y);
    }
    return Qnil;
}
            
int + numeric → numeric_result click to toggle source

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.

 
               VALUE
rb_int_plus(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_plus(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_plus(x, y);
    }
    return rb_num_coerce_bin(x, y, '+');
}
            
int - numeric → numeric_result click to toggle source

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.

 
               VALUE
rb_int_minus(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_minus(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_minus(x, y);
    }
    return rb_num_coerce_bin(x, y, '-');
}
            
-int → integer click to toggle source

Negates int. (returns an integer whose value is 0-int)

 
               VALUE
rb_int_uminus(VALUE num)
{
    if (FIXNUM_P(num)) {
        return fix_uminus(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_uminus(num);
    }
    return num_funcall0(num, idUMinus);
}
            
int / numeric → numeric_result click to toggle source

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.

 
               VALUE
rb_int_div(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_div(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_div(x, y);
    }
    return Qnil;
}
            
int < real → true or false click to toggle source

Returns true if the value of int is less than that of real.

 
               static VALUE
int_lt(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_lt(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_lt(x, y);
    }
    return Qnil;
}
            
int << count → integer click to toggle source

Shifts int left count positions, or right if count is negative.

 
               VALUE
rb_int_lshift(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return rb_fix_lshift(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_lshift(x, y);
    }
    return Qnil;
}
            
int <= real → true or false click to toggle source

Returns true if the value of int is less than or equal to that of real.

 
               static VALUE
int_le(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_le(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_le(x, y);
    }
    return Qnil;
}
            
int <=> numeric → -1, 0, +1 or nil click to toggle source

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.

 
               VALUE
rb_int_cmp(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_cmp(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_cmp(x, y);
    }
    else {
        rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x));
    }
}
            
int == other → true or false click to toggle source

Return true if int equals other numerically. Contrast this with Integer#eql?, which requires other to be a Integer.

1 == 2      #=> false
1 == 1.0    #=> true
 
               VALUE
rb_int_equal(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_equal(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_eq(x, y);
    }
    return Qnil;
}
            
===(p1) click to toggle source
 
               VALUE
rb_int_equal(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_equal(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_eq(x, y);
    }
    return Qnil;
}
            
int > real → true or false click to toggle source

Returns true if the value of int is greater than that of real.

 
               VALUE
rb_int_gt(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_gt(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_gt(x, y);
    }
    return Qnil;
}
            
int >= real → true or false click to toggle source

Returns true if the value of int is greater than or equal to that of real.

 
               VALUE
rb_int_ge(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_ge(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_ge(x, y);
    }
    return Qnil;
}
            
int >> count → integer click to toggle source

Shifts int right count positions, or left if count is negative.

 
               static VALUE
rb_int_rshift(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return rb_fix_rshift(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_rshift(x, y);
    }
    return Qnil;
}
            
int[n] → 0, 1 click to toggle source

Bit Reference—Returns the +n+th 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
 
               static VALUE
int_aref(VALUE num, VALUE idx)
{
    if (FIXNUM_P(num)) {
        return fix_aref(num, idx);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_aref(num, idx);
    }
    return Qnil;
}
            
integer ^ integer → integer_result click to toggle source

Bitwise EXCLUSIVE OR.

 
               static VALUE
int_xor(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_xor(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_xor(x, y);
    }
    return Qnil;
}
            
abs → integer click to toggle source

Returns the absolute value of int.

-12345.abs   #=> 12345
12345.abs    #=> 12345
-1234567890987654321.abs   #=> 1234567890987654321
 
               VALUE
rb_int_abs(VALUE num)
{
    if (FIXNUM_P(num)) {
        return fix_abs(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_abs(num);
    }
    return Qnil;
}
            
bit_length → integer click to toggle source

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
 
               static VALUE
rb_int_bit_length(VALUE num)
{
    if (FIXNUM_P(num)) {
        return rb_fix_bit_length(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_bit_length(num);
    }
    return Qnil;
}
            
ceil([ndigits]) → integer or float click to toggle source

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
 
               static VALUE
int_ceil(int argc, VALUE* argv, VALUE num)
{
    int ndigits;

    if (!rb_check_arity(argc, 0, 1)) return num;
    ndigits = NUM2INT(argv[0]);
    if (ndigits > 0) {
        return rb_Float(num);
    }
    if (ndigits == 0) {
        return num;
    }
    return rb_int_ceil(num, ndigits);
}
            
chr([encoding]) → string click to toggle source

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"
 
               static VALUE
int_chr(int argc, VALUE *argv, VALUE num)
{
    char c;
    unsigned int i;
    rb_encoding *enc;

    if (rb_num_to_uint(num, &i) == 0) {
    }
    else if (FIXNUM_P(num)) {
        rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num));
    }
    else {
        rb_raise(rb_eRangeError, "bignum out of char range");
    }

    switch (argc) {
      case 0:
        if (0xff < i) {
            enc = rb_default_internal_encoding();
            if (!enc) {
                rb_raise(rb_eRangeError, "%d out of char range", i);
            }
            goto decode;
        }
        c = (char)i;
        if (i < 0x80) {
            return rb_usascii_str_new(&c, 1);
        }
        else {
            return rb_str_new(&c, 1);
        }
      case 1:
        break;
      default:
        rb_check_arity(argc, 0, 1);
        break;
    }
    enc = rb_to_encoding(argv[0]);
    if (!enc) enc = rb_ascii8bit_encoding();
  decode:
    return rb_enc_uint_chr(i, enc);
}
            
coerce(numeric) → array click to toggle source

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]
 
               static VALUE
rb_int_coerce(VALUE x, VALUE y)
{
    if (RB_INTEGER_TYPE_P(y)) {
        return rb_assoc_new(y, x);
    }
    else {
        x = rb_Float(x);
        y = rb_Float(y);
        return rb_assoc_new(y, x);
    }
}
            
denominator → 1 click to toggle source

Returns 1.

 
               static VALUE
integer_denominator(VALUE self)
{
    return INT2FIX(1);
}
            
digits → [int] click to toggle source
digits(base) → [int]

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
 
               static VALUE
rb_int_digits(int argc, VALUE *argv, VALUE num)
{
    VALUE base_value;
    long base;

    if (rb_num_negative_p(num))
        rb_raise(rb_eMathDomainError, "out of domain");

    if (rb_check_arity(argc, 0, 1)) {
        base_value = rb_to_int(argv[0]);
        if (!RB_INTEGER_TYPE_P(base_value))
            rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)",
                     rb_obj_classname(argv[0]));
        if (RB_TYPE_P(base_value, T_BIGNUM))
            return rb_int_digits_bigbase(num, base_value);

        base = FIX2LONG(base_value);
        if (base < 0)
            rb_raise(rb_eArgError, "negative radix");
        else if (base < 2)
            rb_raise(rb_eArgError, "invalid radix %ld", base);
    }
    else
        base = 10;

    if (FIXNUM_P(num))
        return rb_fix_digits(num, base);
    else if (RB_TYPE_P(num, T_BIGNUM))
        return rb_int_digits_bigbase(num, LONG2FIX(base));

    return Qnil;
}
            
div(numeric) → integer click to toggle source

Performs integer division: returns integer result of dividing int by numeric.

 
               VALUE
rb_int_idiv(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_idiv(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_idiv(x, y);
    }
    return num_div(x, y);
}
            
divmod(numeric) → array click to toggle source

See Numeric#divmod.

 
               VALUE
rb_int_divmod(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_divmod(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_divmod(x, y);
    }
    return Qnil;
}
            
downto(limit) {|i| block } → self click to toggle source
downto(limit) → an_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!"
 
               static VALUE
int_downto(VALUE from, VALUE to)
{
    RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
    if (FIXNUM_P(from) && FIXNUM_P(to)) {
        long i, end;

        end = FIX2LONG(to);
        for (i=FIX2LONG(from); i >= end; i--) {
            rb_yield(LONG2FIX(i));
        }
    }
    else {
        VALUE i = from, c;

        while (!(c = rb_funcall(i, '<', 1, to))) {
            rb_yield(i);
            i = rb_funcall(i, '-', 1, INT2FIX(1));
        }
        if (NIL_P(c)) rb_cmperr(i, to);
    }
    return from;
}
            
even? → true or false click to toggle source

Returns true if int is an even number.

 
               static VALUE
int_even_p(VALUE num)
{
    if (FIXNUM_P(num)) {
        if ((num & 2) == 0) {
            return Qtrue;
        }
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_even_p(num);
    }
    else if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
        return Qtrue;
    }
    return Qfalse;
}
            
fdiv(numeric) → float click to toggle source

Returns the floating point result of dividing fix by numeric.

654321.fdiv(13731)      #=> 47.6528293642124
654321.fdiv(13731.24)   #=> 47.6519964693647

-1234567890987654321.fdiv(13731)      #=> -89910996357705.5
-1234567890987654321.fdiv(13731.24)   #=> -89909424858035.7
 
               VALUE
rb_int_fdiv(VALUE x, VALUE y)
{
    if (RB_INTEGER_TYPE_P(x)) {
        return DBL2NUM(rb_int_fdiv_double(x, y));
    }
    return Qnil;
}
            
floor([ndigits]) → integer or float click to toggle source

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
 
               static VALUE
int_floor(int argc, VALUE* argv, VALUE num)
{
    int ndigits;

    if (!rb_check_arity(argc, 0, 1)) return num;
    ndigits = NUM2INT(argv[0]);
    if (ndigits > 0) {
        return rb_Float(num);
    }
    if (ndigits == 0) {
        return num;
    }
    return rb_int_floor(num, ndigits);
}
            
gcd(int2) → integer click to toggle source

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
 
               VALUE
rb_gcd(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return f_gcd(self, other);
}
            
gcdlcm(int2) → array click to toggle source

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]
 
               VALUE
rb_gcdlcm(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
}
            
inspect(*args) click to toggle source
Alias for: to_s
integer? → true click to toggle source

Since int is already an Integer, this always returns true.

 
               static VALUE
int_int_p(VALUE num)
{
    return Qtrue;
}
            
lcm(int2) → integer click to toggle source

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
 
               VALUE
rb_lcm(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return f_lcm(self, other);
}
            
magnitude → integer click to toggle source

Returns the absolute value of int.

-12345.abs   #=> 12345
12345.abs    #=> 12345
-1234567890987654321.abs   #=> 1234567890987654321
 
               VALUE
rb_int_abs(VALUE num)
{
    if (FIXNUM_P(num)) {
        return fix_abs(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_abs(num);
    }
    return Qnil;
}
            
modulo(other) → real click to toggle source

Returns int modulo other.

See Numeric#divmod for more information.

 
               VALUE
rb_int_modulo(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_mod(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_modulo(x, y);
    }
    return num_modulo(x, y);
}
            
next → integer click to toggle source

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0
1.succ      #=> 2
(-1).succ   #=> 0
 
               VALUE
rb_int_succ(VALUE num)
{
    if (FIXNUM_P(num)) {
        long i = FIX2LONG(num) + 1;
        return LONG2NUM(i);
    }
    if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_plus(num, INT2FIX(1));
    }
    return num_funcall1(num, '+', INT2FIX(1));
}
            
numerator → self click to toggle source

Returns self.

 
               static VALUE
integer_numerator(VALUE self)
{
    return self;
}
            
odd? → true or false click to toggle source

Returns true if int is an odd number.

 
               static VALUE
int_odd_p(VALUE num)
{
    if (FIXNUM_P(num)) {
        if (num & 2) {
            return Qtrue;
        }
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_odd_p(num);
    }
    else if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
        return Qtrue;
    }
    return Qfalse;
}
            
ord → self click to toggle source

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.

 
               static VALUE
int_ord(VALUE num)
{
    return num;
}
            
pred → integer click to toggle source

Returns the Integer equal to int - 1.

1.pred      #=> 0
(-1).pred   #=> -2
 
               VALUE
rb_int_pred(VALUE num)
{
    if (FIXNUM_P(num)) {
        long i = FIX2LONG(num) - 1;
        return LONG2NUM(i);
    }
    if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_minus(num, INT2FIX(1));
    }
    return num_funcall1(num, '-', INT2FIX(1));
}
            
rationalize([eps]) → rational click to toggle source

Returns the value as a rational. The optional argument eps is always ignored.

 
               static VALUE
integer_rationalize(int argc, VALUE *argv, VALUE self)
{
    rb_scan_args(argc, argv, "01", NULL);
    return integer_to_r(self);
}
            
remainder(numeric) → real click to toggle source

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.

 
               VALUE
int_remainder(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return num_remainder(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_remainder(x, y);
    }
    return Qnil;
}
            
round([ndigits]) → integer or float click to toggle source

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
 
               static VALUE
int_round(int argc, VALUE* argv, VALUE num)
{
    int ndigits;
    int mode;
    VALUE nd, opt;

    if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num;
    ndigits = NUM2INT(nd);
    mode = rb_num_get_rounding_option(opt);
    if (ndigits > 0) {
        return rb_Float(num);
    }
    if (ndigits == 0) {
        return num;
    }
    return rb_int_round(num, ndigits, mode);
}
            
size → int click to toggle source

Returns the number of bytes in the machine representation of fix.

1.size            #=> 4
-1.size           #=> 4
2147483647.size   #=> 4
(256**10 - 1).size   #=> 12
(256**20 - 1).size   #=> 20
(256**40 - 1).size   #=> 40
 
               static VALUE
int_size(VALUE num)
{
    if (FIXNUM_P(num)) {
        return fix_size(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_size_m(num);
    }
    return Qnil;
}
            
succ → integer click to toggle source

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0
1.succ      #=> 2
(-1).succ   #=> 0
 
               VALUE
rb_int_succ(VALUE num)
{
    if (FIXNUM_P(num)) {
        long i = FIX2LONG(num) + 1;
        return LONG2NUM(i);
    }
    if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_plus(num, INT2FIX(1));
    }
    return num_funcall1(num, '+', INT2FIX(1));
}
            
times {|i| block } → self click to toggle source
times → an_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
 
               static VALUE
int_dotimes(VALUE num)
{
    RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);

    if (FIXNUM_P(num)) {
        long i, end;

        end = FIX2LONG(num);
        for (i=0; i<end; i++) {
            rb_yield_1(LONG2FIX(i));
        }
    }
    else {
        VALUE i = INT2FIX(0);

        for (;;) {
            if (!RTEST(rb_funcall(i, '<', 1, num))) break;
            rb_yield(i);
            i = rb_funcall(i, '+', 1, INT2FIX(1));
        }
    }
    return num;
}
            
to_f → float click to toggle source

Converts int to a Float. If int doesn't fit in a Float, the result is infinity.

 
               static VALUE
int_to_f(VALUE num)
{
    double val;

    if (FIXNUM_P(num)) {
        val = (double)FIX2LONG(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        val = rb_big2dbl(num);
    }
    else {
        rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num));
    }

    return DBL2NUM(val);
}
            
to_i → integer click to toggle source

As int is already an Integer, all these methods simply return the receiver.

Synonyms is to_int

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
to_i → integer click to toggle source

As int is already an Integer, all these methods simply return the receiver.

Synonyms is to_int

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
to_r → rational click to toggle source

Returns the value as a rational.

1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)
 
               static VALUE
integer_to_r(VALUE self)
{
    return rb_rational_new1(self);
}
            
to_s(base=10) → string click to toggle source

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"
 
               static VALUE
int_to_s(int argc, VALUE *argv, VALUE x)
{
    int base;

    if (rb_check_arity(argc, 0, 1))
        base = NUM2INT(argv[0]);
    else
        base = 10;
    return rb_int2str(x, base);
}
            
Also aliased as: inspect
truncate([ndigits]) → integer or float click to toggle source

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
 
               static VALUE
int_truncate(int argc, VALUE* argv, VALUE num)
{
    int ndigits;

    if (!rb_check_arity(argc, 0, 1)) return num;
    ndigits = NUM2INT(argv[0]);
    if (ndigits > 0) {
        return rb_Float(num);
    }
    if (ndigits == 0) {
        return num;
    }
    return rb_int_truncate(num, ndigits);
}
            
upto(limit) {|i| block } → self click to toggle source
upto(limit) → an_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
 
               static VALUE
int_upto(VALUE from, VALUE to)
{
    RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size);
    if (FIXNUM_P(from) && FIXNUM_P(to)) {
        long i, end;

        end = FIX2LONG(to);
        for (i = FIX2LONG(from); i <= end; i++) {
            rb_yield(LONG2FIX(i));
        }
    }
    else {
        VALUE i = from, c;

        while (!(c = rb_funcall(i, '>', 1, to))) {
            rb_yield(i);
            i = rb_funcall(i, '+', 1, INT2FIX(1));
        }
        if (NIL_P(c)) rb_cmperr(i, to);
    }
    return from;
}
            
integer | integer → integer_result click to toggle source

Bitwise OR.

 
               static VALUE
int_or(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_or(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_or(x, y);
    }
    return Qnil;
}
            
~integer → integer click to toggle source

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"
 
               static VALUE
int_comp(VALUE num)
{
    if (FIXNUM_P(num)) {
        return fix_comp(num);
    }
    else if (RB_TYPE_P(num, T_BIGNUM)) {
        return rb_big_comp(num);
    }
    return Qnil;
}