Class: Integer

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

Overview

Integer is the basis for the two concrete classes that

hold whole numbers, <code>Bignum</code> and <code>Fixnum</code>.

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

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

Overloads:



2290
2291
2292
2293
2294
# File 'numeric.c', line 2290

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:



2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
# File 'numeric.c', line 2434

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)


1791
1792
1793
1794
1795
# File 'rational.c', line 1791

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

#downto(limit) {|i| ... } ⇒ self #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:

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

    Yields:

    • (i)

    Returns:

    • (self)


3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
# File 'numeric.c', line 3510

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)


2332
2333
2334
2335
2336
2337
2338
2339
# File 'numeric.c', line 2332

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

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

Overloads:



2290
2291
2292
2293
2294
# File 'numeric.c', line 2290

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:



1675
1676
1677
1678
1679
1680
# File 'rational.c', line 1675

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:



1710
1711
1712
1713
1714
1715
# File 'rational.c', line 1710

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)

Returns:

  • (Boolean)


2303
2304
2305
2306
2307
# File 'numeric.c', line 2303

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:



1693
1694
1695
1696
1697
1698
# File 'rational.c', line 1693

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:



2370
2371
2372
2373
2374
2375
2376
2377
2378
# File 'numeric.c', line 2370

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

#numeratorself

Returns self.

Returns:

  • (self)


1779
1780
1781
1782
1783
# File 'rational.c', line 1779

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

#odd?Boolean

Returns true if int is an odd number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


2316
2317
2318
2319
2320
2321
2322
2323
# File 'numeric.c', line 2316

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)


2491
2492
2493
2494
2495
# File 'numeric.c', line 2491

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

#predInteger

Returns the Integer equal to int - 1.

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

Returns:



2390
2391
2392
2393
2394
2395
2396
2397
2398
# File 'numeric.c', line 2390

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. The optional argument eps is always ignored.



1882
1883
1884
1885
1886
1887
# File 'rational.c', line 1882

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:



3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
# File 'numeric.c', line 3603

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

#nextInteger #succInteger

Returns the Integer equal to int + 1.

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

Overloads:



2370
2371
2372
2373
2374
2375
2376
2377
2378
# File 'numeric.c', line 2370

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| ... } ⇒ self #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:

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

    Yields:

    • (i)

    Returns:

    • (self)


3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
# File 'numeric.c', line 3565

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 #to_intInteger #floorInteger #ceilInteger #truncateInteger

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

Overloads:



2290
2291
2292
2293
2294
# File 'numeric.c', line 2290

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

#to_iInteger #to_intInteger #floorInteger #ceilInteger #truncateInteger

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

Overloads:



2290
2291
2292
2293
2294
# File 'numeric.c', line 2290

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)


1869
1870
1871
1872
1873
# File 'rational.c', line 1869

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

#to_iInteger #to_intInteger #floorInteger #ceilInteger #truncateInteger

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

Overloads:



2290
2291
2292
2293
2294
# File 'numeric.c', line 2290

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

#upto(limit) {|i| ... } ⇒ self #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:

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

    Yields:

    • (i)

    Returns:

    • (self)


3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
# File 'numeric.c', line 3462

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