class Symbol

BigDecimal extends the native Integer class to provide the to_d method.

When you require the BigDecimal library in your application, this methodwill be available on Integer objects.

Add double dispatch to Integer

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

Public Class Methods

each_prime(ubound) { |prime| ... } click to toggle source

Iterates the given block over all prime numbers.

See Prime#each for more details.

# File lib/prime.rb, line 48
def Integer.each_prime(ubound, &block) # :yields: prime
  Prime.each(ubound, &block)
end
from_prime_division(pd) click to toggle source

Re-composes a prime factorization and returns the product.

See Prime#int_from_prime_division for more details.

# File lib/prime.rb, line 22
def Integer.from_prime_division(pd)
  Prime.int_from_prime_division(pd)
end

Public Instance Methods

to_i → integer click to toggle source

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

Synonyms are to_int, floor, ceil, truncate.

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 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);
}
denominator → 1 click to toggle source

Returns 1.

static VALUE
integer_denominator(VALUE self)
{
    return INT2FIX(1);
}
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 (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
        return Qtrue;
    }
    return Qfalse;
}
to_i → integer click to toggle source

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

Synonyms are to_int, floor, ceil, truncate.

static VALUE
int_to_i(VALUE num)
{
    return num;
}
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));
}
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);
}
next → integer click to toggle source

Returns the Integer equal to int + 1, same as Fixnum#next.

1.next      #=> 2
(-1).next   #=> 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 rb_funcall(num, '+', 1, 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 (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 rb_funcall(num, '-', 1, INT2FIX(1));
}
prime?() click to toggle source

Returns true if self is a prime number, else returns false.

# File lib/prime.rb, line 34
def prime?
  return self >= 2 if self <= 3
  return false if self % 2 == 0 or self % 3 == 0
  (5..(self**0.5).floor).step(6).each do |i|
    if self % i == 0 || self % (i + 2) == 0
      return false
    end
  end
  true
end
prime_division(generator = Prime::Generator23.new) click to toggle source

Returns the factorization of self.

See Prime#prime_division for more details.

# File lib/prime.rb, line 29
def prime_division(generator = Prime::Generator23.new)
  Prime.prime_division(self, generator)
end
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);
}
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)
{
    VALUE n;
    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;
    }
    return int_round_0(num, ndigits);
}
succ → integer click to toggle source

Returns the Integer equal to int + 1, same as Fixnum#next.

1.next      #=> 2
(-1).next   #=> 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 rb_funcall(num, '+', 1, 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_bn() click to toggle source

Casts an Integer as an OpenSSL::BN

See `man bn` for more info.

# File ext/openssl/lib/openssl/bn.rb, line 36
def to_bn
  OpenSSL::BN::new(self)
end
to_d → bigdecimal click to toggle source

Convert int to a BigDecimal and return it.

require 'bigdecimal'
require 'bigdecimal/util'

42.to_d
# => #<BigDecimal:1008ef070,'0.42E2',9(36)>
# File ext/bigdecimal/lib/bigdecimal/util.rb, line 18
def to_d
  BigDecimal(self)
end
to_i → integer click to toggle source

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

Synonyms are to_int, floor, ceil, truncate.

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 are to_int, floor, ceil, truncate.

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_i → integer click to toggle source

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

Synonyms are to_int, floor, ceil, truncate.

static VALUE
int_to_i(VALUE num)
{
    return num;
}
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;
}