Class: Integer

Inherits:
Numeric show all
Defined in:
numeric.c,
numeric.c

Overview

This class 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

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

Synonyms are #to_int, #floor, #ceil, #truncate.

Returns:



2393
2394
2395
2396
2397
# File 'numeric.c', line 2393

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

#chr([encoding]) ⇒ String

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"

Returns:



2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
# File 'numeric.c', line 2547

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

#denominator1

Returns 1.

Returns:

  • (1)


1858
1859
1860
1861
1862
# File 'rational.c', line 1858

static VALUE
integer_denominator(VALUE self)
{
    return INT2FIX(1);
}

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

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

Overloads:

  • #downto(limit) {|i| ... } ⇒ self

    Yields:

    • (i)

    Returns:

    • (self)


3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
# File 'numeric.c', line 3658

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?Boolean

Returns true if int is an even number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


2435
2436
2437
2438
2439
2440
2441
2442
# File 'numeric.c', line 2435

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

#to_iInteger

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

Synonyms are #to_int, #floor, #ceil, #truncate.

Returns:



2393
2394
2395
2396
2397
# File 'numeric.c', line 2393

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).

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

Returns:



1713
1714
1715
1716
1717
1718
# File 'rational.c', line 1713

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)].

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

Returns:



1748
1749
1750
1751
1752
1753
# File 'rational.c', line 1748

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

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

Returns:

  • (true)

Returns:

  • (Boolean)


2406
2407
2408
2409
2410
# File 'numeric.c', line 2406

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.

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

Returns:



1731
1732
1733
1734
1735
1736
# File 'rational.c', line 1731

VALUE
rb_lcm(VALUE self, VALUE other)
{
    other = nurat_int_value(other);
    return f_lcm(self, other);
}

#nextObject

#numeratorself

Returns self.

Returns:

  • (self)


1846
1847
1848
1849
1850
# File 'rational.c', line 1846

static VALUE
integer_numerator(VALUE self)
{
    return self;
}

#odd?Boolean

Returns true if int is an odd number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


2419
2420
2421
2422
2423
2424
2425
2426
# File 'numeric.c', line 2419

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

#ordself

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:

  • (self)


2605
2606
2607
2608
2609
# File 'numeric.c', line 2605

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

#predObject

#rationalize([eps]) ⇒ Object

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



1949
1950
1951
1952
1953
1954
# File 'rational.c', line 1949

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

Returns:



3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
# File 'numeric.c', line 3749

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

#succObject

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

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

Overloads:

  • #times {|i| ... } ⇒ self

    Yields:

    • (i)

    Returns:

    • (self)


3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
# File 'numeric.c', line 3710

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(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

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

Synonyms are #to_int, #floor, #ceil, #truncate.

Returns:



2393
2394
2395
2396
2397
# File 'numeric.c', line 2393

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

#to_iInteger

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

Synonyms are #to_int, #floor, #ceil, #truncate.

Returns:



2393
2394
2395
2396
2397
# File 'numeric.c', line 2393

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

#to_rObject

Returns the value as a rational.

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


1936
1937
1938
1939
1940
# File 'rational.c', line 1936

static VALUE
integer_to_r(VALUE self)
{
    return rb_rational_new1(self);
}

#to_iInteger

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

Synonyms are #to_int, #floor, #ceil, #truncate.

Returns:



2393
2394
2395
2396
2397
# File 'numeric.c', line 2393

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

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

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

Overloads:

  • #upto(limit) {|i| ... } ⇒ self

    Yields:

    • (i)

    Returns:

    • (self)


3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
# File 'numeric.c', line 3613

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