Class: Integer

Inherits:
Numeric show all
Defined in:
numeric.c

Overview

Integer is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.

Direct Known Subclasses

Bignum, Fixnum

Instance Method Summary collapse

Methods inherited from Numeric

#%, #+@, #-@, #<=>, #abs, #abs2, #angle, #arg, #coerce, #conj, #conjugate, #div, #divmod, #eql?, #fdiv, #i, #imag, #imaginary, #initialize_copy, #magnitude, #modulo, #nonzero?, #phase, #polar, #quo, #real, #real?, #rect, #rectangular, #remainder, #singleton_method_added, #step, #to_c, #zero?

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?

Instance Method Details

#to_iInteger #to_intInteger #floorInteger #ceilInteger #roundInteger #truncateInteger

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

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.to_i      ->  integer
 *     int.to_int    ->  integer
 *     int.floor     ->  integer
 *     int.ceil      ->  integer
 *     int.round     ->  integer
 *     int.truncate  ->  integer
 *
 *  As <i>int</i> is already an <code>Integer</code>, all these
 *  methods simply return the receiver.
 */

static VALUE
int_to_i(VALUE num)
{
    return num;
}

#chr([encoding]) ⇒ String

Returns a string containing the character represented by the receiver's value according to encoding.

65.chr    #=> "A"
230.chr   #=> "\346"
255.chr(Encoding::UTF_8)   #=> "\303\277"

Returns:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.chr([encoding])  ->  string
 *
 *  Returns a string containing the character represented by the
 *  receiver'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 = NUM2UINT(num);
    rb_encoding *enc;

    switch (argc) {
      case 0:
    if (i < 0) {
      out_of_range:
        rb_raise(rb_eRangeError, "%d out of char range", i);
    }
    if (0xff < i) {
        enc = rb_default_internal_encoding();
        if (!enc) goto out_of_range;
        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_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
    break;
    }
    enc = rb_to_encoding(argv[0]);
    if (!enc) enc = rb_ascii8bit_encoding();
  decode:
    return rb_enc_uint_chr(i, enc);
}

#denominator1

Returns 1.

Returns:

  • (1)


# File 'rational.c'

/*
 * call-seq:
 *    int.denominator  ->  1
 *
 * Returns 1.
 */
static VALUE
integer_denominator(VALUE self)
{
    return INT2FIX(1);
}

#downto(limit) {|i| ... } ⇒ Integer #downto(limit) ⇒ Object

Iterates 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"

produces:

5.. 4.. 3.. 2.. 1..   Liftoff!

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.downto(limit) {|i| block }  ->  self
 *     int.downto(limit)               ->  an_enumerator
 *
 *  Iterates <em>block</em>, passing decreasing values from <i>int</i>
 *  down to and including <i>limit</i>.
 *
 *  If no block is given, an enumerator is returned instead.
 *
 *     5.downto(1) { |n| print n, ".. " }
 *     print "  Liftoff!\n"
 *
 *  <em>produces:</em>
 *
 *     5.. 4.. 3.. 2.. 1..   Liftoff!
 */

static VALUE
int_downto(VALUE from, VALUE to)
{
    RETURN_ENUMERATOR(from, 1, &to);
    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?Boolean

Returns true if int is an even number.

Returns:

  • (Boolean)


# File 'numeric.c'

/*
 *  call-seq:
 *     int.even?  ->  true or false
 *
 *  Returns <code>true</code> if <i>int</i> is an even number.
 */

static VALUE
int_even_p(VALUE num)
{
    if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
    return Qtrue;
    }
    return Qfalse;
}

#to_iInteger #to_intInteger #floorInteger #ceilInteger #roundInteger #truncateInteger

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

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.to_i      ->  integer
 *     int.to_int    ->  integer
 *     int.floor     ->  integer
 *     int.ceil      ->  integer
 *     int.round     ->  integer
 *     int.truncate  ->  integer
 *
 *  As <i>int</i> is already an <code>Integer</code>, all these
 *  methods simply return the receiver.
 */

static VALUE
int_to_i(VALUE num)
{
    return num;
}

#gcd(int2) ⇒ Integer

Returns the greatest common divisor (always positive). 0.gcd(x) and x.gcd(0) return abs(x).

For example:

2.gcd(2)                    #=> 2
3.gcd(-7)                   #=> 1
((1<<31)-1).gcd((1<<61)-1)  #=> 1

Returns:



# File 'rational.c'

/*
 * call-seq:
 *    int.gcd(int2)  ->  integer
 *
 * Returns the greatest common divisor (always positive).  0.gcd(x)
 * and x.gcd(0) return abs(x).
 *
 * For example:
 *
 *    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

Returns an array; [int.gcd(int2), int.lcm(int2)].

For example:

2.gcdlcm(2)                    #=> [2, 2]
3.gcdlcm(-7)                   #=> [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1)  #=> [1, 4951760154835678088235319297]

Returns:



# File 'rational.c'

/*
 * call-seq:
 *    int.gcdlcm(int2)  ->  array
 *
 * Returns an array; [int.gcd(int2), int.lcm(int2)].
 *
 * For example:
 *
 *    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));
}

#integer?true

Always returns true.

Returns:

  • (true)


# File 'numeric.c'

/*
 *  call-seq:
 *     int.integer?  ->  true
 *
 *  Always returns <code>true</code>.
 */

static VALUE
int_int_p(VALUE num)
{
    return Qtrue;
}

#lcm(int2) ⇒ Integer

Returns the least common multiple (always positive). 0.lcm(x) and x.lcm(0) return zero.

For example:

2.lcm(2)                    #=> 2
3.lcm(-7)                   #=> 21
((1<<31)-1).lcm((1<<61)-1)  #=> 4951760154835678088235319297

Returns:



# File 'rational.c'

/*
 * call-seq:
 *    int.lcm(int2)  ->  integer
 *
 * Returns the least common multiple (always positive).  0.lcm(x) and
 * x.lcm(0) return zero.
 *
 * For example:
 *
 *    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);
}

#nextInteger #succInteger

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.next  ->  integer
 *     int.succ  ->  integer
 *
 *  Returns the <code>Integer</code> equal to <i>int</i> + 1.
 *
 *     1.next      #=> 2
 *     (-1).next   #=> 0
 */

static VALUE
int_succ(VALUE num)
{
    if (FIXNUM_P(num)) {
    long i = FIX2LONG(num) + 1;
    return LONG2NUM(i);
    }
    return rb_funcall(num, '+', 1, INT2FIX(1));
}

#numeratorInteger

Returns self.

Returns:



# File 'rational.c'

/*
 * call-seq:
 *    int.numerator  ->  self
 *
 * Returns self.
 */
static VALUE
integer_numerator(VALUE self)
{
    return self;
}

#odd?Boolean

Returns true if int is an odd number.

Returns:

  • (Boolean)


# File 'numeric.c'

/*
 *  call-seq:
 *     int.odd?  ->  true or false
 *
 *  Returns <code>true</code> if <i>int</i> is an odd number.
 */

static VALUE
int_odd_p(VALUE num)
{
    if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
    return Qtrue;
    }
    return Qfalse;
}

#ordInteger

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.

Returns:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.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.
 */

static VALUE
int_ord(num)
    VALUE num;
{
    return num;
}

#predInteger

Returns the Integer equal to int - 1.

1.pred      #=> 0
(-1).pred   #=> -2

Returns:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.pred  ->  integer
 *
 *  Returns the <code>Integer</code> equal to <i>int</i> - 1.
 *
 *     1.pred      #=> 0
 *     (-1).pred   #=> -2
 */

static VALUE
int_pred(VALUE num)
{
    if (FIXNUM_P(num)) {
    long i = FIX2LONG(num) - 1;
    return LONG2NUM(i);
    }
    return rb_funcall(num, '-', 1, INT2FIX(1));
}

#rationalize([eps]) ⇒ Object

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



# File 'rational.c'

/*
 * call-seq:
 *    int.rationalize([eps])  ->  rational
 *
 * Returns the value as a rational.  An 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);
}

#round([ndigits]) ⇒ Integer, Float

Rounds flt 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

Returns:



# File 'numeric.c'

/*
 *  call-seq:
 *     num.round([ndigits])  ->  integer or float
 *
 *  Rounds <i>flt</i> 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)
{
    VALUE n, f, h, r;
    int ndigits;

    if (argc == 0) return num;
    rb_scan_args(argc, argv, "1", &n);
    ndigits = NUM2INT(n);
    if (ndigits > 0) {
    return rb_Float(num);
    }
    if (ndigits == 0) {
    return num;
    }
    ndigits = -ndigits;
    if (ndigits < 0) {
    rb_raise(rb_eArgError, "ndigits out of range");
    }
    f = int_pow(10, ndigits);
    if (FIXNUM_P(num) && FIXNUM_P(f)) {
    SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
    int neg = x < 0;
    if (neg) x = -x;
    x = (x + y / 2) / y * y;
    if (neg) x = -x;
    return LONG2NUM(x);
    }
    h = rb_funcall(f, '/', 1, INT2FIX(2));
    r = rb_funcall(num, '%', 1, f);
    n = rb_funcall(num, '-', 1, r);
    if (!RTEST(rb_funcall(r, '<', 1, h))) {
    n = rb_funcall(n, '+', 1, f);
    }
    return n;
}

#nextInteger #succInteger

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.next  ->  integer
 *     int.succ  ->  integer
 *
 *  Returns the <code>Integer</code> equal to <i>int</i> + 1.
 *
 *     1.next      #=> 2
 *     (-1).next   #=> 0
 */

static VALUE
int_succ(VALUE num)
{
    if (FIXNUM_P(num)) {
    long i = FIX2LONG(num) + 1;
    return LONG2NUM(i);
    }
    return rb_funcall(num, '+', 1, INT2FIX(1));
}

#times {|i| ... } ⇒ Integer #timesObject

Iterates 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

produces:

0 1 2 3 4

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.times {|i| block }  ->  self
 *     int.times               ->  an_enumerator
 *
 *  Iterates block <i>int</i> times, passing in values from zero to
 *  <i>int</i> - 1.
 *
 *  If no block is given, an enumerator is returned instead.
 *
 *     5.times do |i|
 *       print i, " "
 *     end
 *
 *  <em>produces:</em>
 *
 *     0 1 2 3 4
 */

static VALUE
int_dotimes(VALUE num)
{
    RETURN_ENUMERATOR(num, 0, 0);

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

    end = FIX2LONG(num);
    for (i=0; i<end; i++) {
        rb_yield(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_iInteger #to_intInteger #floorInteger #ceilInteger #roundInteger #truncateInteger

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

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.to_i      ->  integer
 *     int.to_int    ->  integer
 *     int.floor     ->  integer
 *     int.ceil      ->  integer
 *     int.round     ->  integer
 *     int.truncate  ->  integer
 *
 *  As <i>int</i> is already an <code>Integer</code>, all these
 *  methods simply return the receiver.
 */

static VALUE
int_to_i(VALUE num)
{
    return num;
}

#to_iInteger #to_intInteger #floorInteger #ceilInteger #roundInteger #truncateInteger

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

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.to_i      ->  integer
 *     int.to_int    ->  integer
 *     int.floor     ->  integer
 *     int.ceil      ->  integer
 *     int.round     ->  integer
 *     int.truncate  ->  integer
 *
 *  As <i>int</i> is already an <code>Integer</code>, all these
 *  methods simply return the receiver.
 */

static VALUE
int_to_i(VALUE num)
{
    return num;
}

#to_rObject

Returns the value as a rational.

For example:

1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)


# File 'rational.c'

/*
 * call-seq:
 *    int.to_r  ->  rational
 *
 * Returns the value as a rational.
 *
 * For example:
 *
 *    1.to_r        #=> (1/1)
 *    (1<<64).to_r  #=> (18446744073709551616/1)
 */
static VALUE
integer_to_r(VALUE self)
{
    return rb_rational_new1(self);
}

#to_iInteger #to_intInteger #floorInteger #ceilInteger #roundInteger #truncateInteger

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

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.to_i      ->  integer
 *     int.to_int    ->  integer
 *     int.floor     ->  integer
 *     int.ceil      ->  integer
 *     int.round     ->  integer
 *     int.truncate  ->  integer
 *
 *  As <i>int</i> is already an <code>Integer</code>, all these
 *  methods simply return the receiver.
 */

static VALUE
int_to_i(VALUE num)
{
    return num;
}

#upto(limit) {|i| ... } ⇒ Integer #upto(limit) ⇒ Object

Iterates block, passing in integer values from int up to and including limit.

If no block is given, an enumerator is returned instead.

5.upto(10) { |i| print i, " " }

produces:

5 6 7 8 9 10

Overloads:



# File 'numeric.c'

/*
 *  call-seq:
 *     int.upto(limit) {|i| block }  ->  self
 *     int.upto(limit)               ->  an_enumerator
 *
 *  Iterates <em>block</em>, passing in integer values from <i>int</i>
 *  up to and including <i>limit</i>.
 *
 *  If no block is given, an enumerator is returned instead.
 *
 *     5.upto(10) { |i| print i, " " }
 *
 *  <em>produces:</em>
 *
 *     5 6 7 8 9 10
 */

static VALUE
int_upto(VALUE from, VALUE to)
{
    RETURN_ENUMERATOR(from, 1, &to);
    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;
}