# 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.``

## Instance Method Summary collapse

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

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

• Returns 1.

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

• Returns true if int is an even number.

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

• Returns the greatest common divisor (always positive).

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

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

• Returns the least common multiple (always positive).

• Returns self.

• Returns true if int is an odd number.

• Returns the int itself.

• Returns the value as a rational.

• Rounds int to a given precision in decimal digits (default 0 digits).

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

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

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

• Returns the value as a rational.

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

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

## Instance Method Details

### #to_i ⇒ Integer

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

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

Returns:

 ``` 2611 2612 2613 2614 2615``` ```# File 'numeric.c', line 2611 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:

 ``` 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807``` ```# File 'numeric.c', line 2765 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

Returns 1.

Returns:

• (1)
 ``` 1872 1873 1874 1875 1876``` ```# File 'rational.c', line 1872 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!"``````

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

Yields:

• (i)

Returns:

• (self)
 ``` 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913``` ```# File 'numeric.c', line 3891 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)
 ``` 2653 2654 2655 2656 2657 2658 2659 2660``` ```# File 'numeric.c', line 2653 static VALUE int_even_p(VALUE num) { if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) { return Qtrue; } return Qfalse; }```

### #to_i ⇒ Integer

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

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

Returns:

 ``` 2611 2612 2613 2614 2615``` ```# File 'numeric.c', line 2611 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:

 ``` 1715 1716 1717 1718 1719 1720``` ```# File 'rational.c', line 1715 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:

 ``` 1750 1751 1752 1753 1754 1755``` ```# File 'rational.c', line 1750 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)
 ``` 2624 2625 2626 2627 2628``` ```# File 'numeric.c', line 2624 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:

 ``` 1733 1734 1735 1736 1737 1738``` ```# File 'rational.c', line 1733 VALUE rb_lcm(VALUE self, VALUE other) { other = nurat_int_value(other); return f_lcm(self, other); }```

### #numerator ⇒ self

Returns self.

Returns:

• (self)
 ``` 1860 1861 1862 1863 1864``` ```# File 'rational.c', line 1860 static VALUE integer_numerator(VALUE self) { return self; }```

### #odd? ⇒ Boolean

Returns true if int is an odd number.

Returns:

• (Boolean)

Returns:

• (Boolean)
 ``` 2637 2638 2639 2640 2641 2642 2643 2644``` ```# File 'numeric.c', line 2637 static VALUE int_odd_p(VALUE num) { if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) { return Qtrue; } return Qfalse; }```

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

Returns:

• (self)
 ``` 2823 2824 2825 2826 2827``` ```# File 'numeric.c', line 2823 static VALUE int_ord(VALUE num) { return num; }```

### #rationalize([eps]) ⇒ Object

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

 ``` 1963 1964 1965 1966 1967 1968``` ```# File 'rational.c', line 1963 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:

 ``` 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998``` ```# File 'numeric.c', line 3982 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); }```

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

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

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

Yields:

• (i)

Returns:

• (self)
 ``` 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966``` ```# File 'numeric.c', line 3943 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

### #to_i ⇒ Integer

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

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

Returns:

 ``` 2611 2612 2613 2614 2615``` ```# File 'numeric.c', line 2611 static VALUE int_to_i(VALUE num) { return num; }```

### #to_i ⇒ Integer

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

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

Returns:

 ``` 2611 2612 2613 2614 2615``` ```# File 'numeric.c', line 2611 static VALUE int_to_i(VALUE num) { return num; }```

### #to_r ⇒ Object

Returns the value as a rational.

``````1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)``````
 ``` 1950 1951 1952 1953 1954``` ```# File 'rational.c', line 1950 static VALUE integer_to_r(VALUE self) { return rb_rational_new1(self); }```

### #to_i ⇒ Integer

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

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

Returns:

 ``` 2611 2612 2613 2614 2615``` ```# File 'numeric.c', line 2611 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``````

 ``` 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868``` ```# File 'numeric.c', line 3846 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; }```