Class: Integer
Overview
This class is the basis for the two concrete classes that hold whole
numbers, Bignum and Fixnum.
Instance Method Summary collapse
-
#to_i ⇒ Integer
As
int
is already an Integer, all these methods simply return the receiver. -
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the
int
‘s value according toencoding
. -
#denominator ⇒ 1
Returns 1.
-
#downto(to) ⇒ Object
Iterates the given block, passing decreasing values from
int
down to and includinglimit
. -
#even? ⇒ Boolean
Returns
true
ifint
is an even number. -
#to_i ⇒ Integer
As
int
is already an Integer, all these methods simply return the receiver. -
#gcd(int2) ⇒ Integer
Returns the greatest common divisor (always positive).
-
#gcdlcm(int2) ⇒ Array
Returns an array; [int.gcd(int2), int.lcm(int2)].
-
#integer? ⇒ true
Since
int
is already an Integer, this always returnstrue
. -
#lcm(int2) ⇒ Integer
Returns the least common multiple (always positive).
- #next ⇒ Object
-
#numerator ⇒ self
Returns self.
-
#odd? ⇒ Boolean
Returns
true
ifint
is an odd number. -
#ord ⇒ self
Returns the
int
itself. - #pred ⇒ Object
-
#rationalize([eps]) ⇒ Object
Returns the value as a rational.
-
#round([ndigits]) ⇒ Integer, Float
Rounds
int
to a given precision in decimal digits (default 0 digits). - #succ ⇒ Object
-
#times ⇒ Object
Iterates the given block
int
times, passing in values from zero toint - 1
. -
#to_i ⇒ Integer
As
int
is already an Integer, all these methods simply return the receiver. -
#to_i ⇒ Integer
As
int
is already an Integer, all these methods simply return the receiver. -
#to_r ⇒ Object
Returns the value as a rational.
-
#to_i ⇒ Integer
As
int
is already an Integer, all these methods simply return the receiver. -
#upto(to) ⇒ Object
Iterates the given block, passing in integer values from
int
up to and includinglimit
.
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_i ⇒ Integer
As int
is already an Integer, all these methods simply return the receiver.
Synonyms are #to_int, #floor, #ceil, #truncate.
2530 2531 2532 2533 2534 |
# File 'numeric.c', line 2530
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"
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 |
# File 'numeric.c', line 2684
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.
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!"
3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 |
# File 'numeric.c', line 3802
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.
2572 2573 2574 2575 2576 2577 2578 2579 |
# File 'numeric.c', line 2572
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.
2530 2531 2532 2533 2534 |
# File 'numeric.c', line 2530
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
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]
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
.
2543 2544 2545 2546 2547 |
# File 'numeric.c', line 2543
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
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);
}
|
#next ⇒ Object
#numerator ⇒ 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.
2556 2557 2558 2559 2560 2561 2562 2563 |
# File 'numeric.c', line 2556
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.
2742 2743 2744 2745 2746 |
# File 'numeric.c', line 2742
static VALUE
int_ord(VALUE num)
{
return num;
}
|
#pred ⇒ Object
#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
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 |
# File 'numeric.c', line 3893
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 ⇒ Object
#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
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 |
# File 'numeric.c', line 3854
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_i ⇒ Integer
As int
is already an Integer, all these methods simply return the receiver.
Synonyms are #to_int, #floor, #ceil, #truncate.
2530 2531 2532 2533 2534 |
# File 'numeric.c', line 2530
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.
2530 2531 2532 2533 2534 |
# File 'numeric.c', line 2530
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.
2530 2531 2532 2533 2534 |
# File 'numeric.c', line 2530
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
3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 |
# File 'numeric.c', line 3757
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;
}
|