In Files

  • numeric.c
  • rational.c

Integer

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

Public Instance Methods

to_i => int click to toggle source
to_int => int
floor => int
ceil => int
round => int
truncate => int

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

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
chr([encoding]) => string click to toggle source

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;
    int n;
    long i = NUM2LONG(num);
    rb_encoding *enc;
    VALUE str;

    switch (argc) {
      case 0:
        if (i < 0 || 0xff < i) {
          out_of_range:
            rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
        }
        c = 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 or 1)", argc);
        break;
    }
    enc = rb_to_encoding(argv[0]);
    if (!enc) enc = rb_ascii8bit_encoding();
    if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range;
    str = rb_enc_str_new(0, n, enc);
    rb_enc_mbcput(i, RSTRING_PTR(str), enc);
    return str;
}
            
denominator() click to toggle source
 
               static VALUE
integer_denominator(VALUE self)
{
    return INT2FIX(1);
}
            
downto(limit) {|i| block } => int click to toggle source

Iterates block, passing decreasing values from int down to and including limit.

5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"

produces:

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? → true or false click to toggle source

Returns true if int 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_i => int click to toggle source
to_int => int
floor => int
ceil => int
round => int
truncate => int

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

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
gcd(p1) click to toggle source

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

Always returns true.

 
               static VALUE
int_int_p(VALUE num)
{
    return Qtrue;
}
            
lcm(p1) click to toggle source
 
               VALUE
rb_lcm(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return f_lcm(self, other);
}
            
next => integer click to toggle source
succ => integer

Returns the Integer equal to int + 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));
}
            
numerator() click to toggle source
 
               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 (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
        return Qtrue;
    }
    return Qfalse;
}
            
ord => int 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(num)
    VALUE num;
{
    return num;
}
            
pred => integer click to toggle source

Returns the Integer equal to int - 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));
}
            
round(p1) click to toggle source
 
               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;
}
            
next => integer click to toggle source
succ => integer

Returns the Integer equal to int + 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| block } => int click to toggle source

Iterates block int times, passing in values from zero to int - 1.

5.times do |i|
  print i, " "
end

produces:

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_i => int click to toggle source
to_int => int
floor => int
ceil => int
round => int
truncate => int

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

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
to_i => int click to toggle source
to_int => int
floor => int
ceil => int
round => int
truncate => int

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

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
to_r() click to toggle source
 
               static VALUE
integer_to_r(VALUE self)
{
    return rb_rational_new1(self);
}
            
to_i => int click to toggle source
to_int => int
floor => int
ceil => int
round => int
truncate => int

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

 
               static VALUE
int_to_i(VALUE num)
{
    return num;
}
            
upto(limit) {|i| block } => int click to toggle source

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

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

produces:

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;
}