Class: Integer
- Includes:
- Precision
- 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>.
Class Method Summary collapse
-
.induced_from(obj) ⇒ Fixnum
Convert
obj
to an Integer.
Instance Method Summary collapse
-
#ceil ⇒ Object
As int is already an
Integer
, all these methods simply return the receiver. -
#chr ⇒ String
Returns a string containing the ASCII character represented by the receiver’s value.
-
#downto(limit) {|i| ... } ⇒ Integer
Iterates block, passing decreasing values from int down to and including limit.
-
#even? ⇒ Boolean
Returns
true
if int is an even number. -
#floor ⇒ Object
As int is already an
Integer
, all these methods simply return the receiver. -
#integer? ⇒ true
Always returns
true
. -
#next ⇒ Object
Returns the
Integer
equal to int + 1. -
#odd? ⇒ Boolean
Returns
true
if int is an odd number. -
#ord ⇒ Integer
Returns the int itself.
-
#pred ⇒ Integer
Returns the
Integer
equal to int - 1. -
#round ⇒ Object
As int is already an
Integer
, all these methods simply return the receiver. -
#succ ⇒ Object
Returns the
Integer
equal to int + 1. -
#times {|i| ... } ⇒ Integer
Iterates block int times, passing in values from zero to int - 1.
-
#to_i ⇒ Object
As int is already an
Integer
, all these methods simply return the receiver. -
#to_int ⇒ Object
As int is already an
Integer
, all these methods simply return the receiver. -
#truncate ⇒ Object
As int is already an
Integer
, all these methods simply return the receiver. -
#upto(limit) {|i| ... } ⇒ Integer
Iterates block, passing in integer values from int up to and including limit.
Methods included from Precision
included, #prec, #prec_f, #prec_i
Methods inherited from Numeric
#+@, #-@, #<=>, #abs, #coerce, #div, #divmod, #eql?, #fdiv, #initialize_copy, #modulo, #nonzero?, #quo, #remainder, #singleton_method_added, #step, #zero?
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?
Class Method Details
.induced_from(obj) ⇒ Fixnum
Convert obj
to an Integer.
1948 1949 1950 |
# File 'numeric.c', line 1948 static VALUE rb_int_induced_from(klass, x) VALUE klass, x; |
Instance Method Details
#to_i ⇒ Integer #to_int ⇒ Integer #floor ⇒ Integer #ceil ⇒ Integer #round ⇒ Integer #truncate ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1766 1767 1768 |
# File 'numeric.c', line 1766 static VALUE int_to_i(num) VALUE num; |
#chr ⇒ String
Returns a string containing the ASCII character represented by the receiver’s value.
65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"
1874 1875 1876 |
# File 'numeric.c', line 1874 static VALUE int_chr(num) VALUE num; |
#downto(limit) {|i| ... } ⇒ Integer
Iterates block, passing decreasing values from int down to and including limit.
5.downto(1) { |n| print n, ".. " }
print " Liftoff!\n"
produces:
5.. 4.. 3.. 2.. 1.. Liftoff!
2884 2885 2886 |
# File 'numeric.c', line 2884 static VALUE int_downto(from, to) VALUE from, to; |
#even? ⇒ Boolean
Returns true
if int is an even number.
1810 1811 1812 1813 1814 1815 1816 1817 |
# File 'numeric.c', line 1810
static VALUE
int_even_p(VALUE num)
{
if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
return Qtrue;
}
return Qfalse;
}
|
#to_i ⇒ Integer #to_int ⇒ Integer #floor ⇒ Integer #ceil ⇒ Integer #round ⇒ Integer #truncate ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1766 1767 1768 |
# File 'numeric.c', line 1766 static VALUE int_to_i(num) VALUE num; |
#integer? ⇒ true
Always returns true
.
1780 1781 1782 |
# File 'numeric.c', line 1780 static VALUE int_int_p(num) VALUE num; |
#next ⇒ Integer #succ ⇒ Integer
Returns the Integer
equal to int + 1.
1.next #=> 2
(-1).next #=> 0
1831 1832 1833 |
# File 'numeric.c', line 1831 static VALUE int_succ(num) VALUE num; |
#odd? ⇒ Boolean
Returns true
if int is an odd number.
1794 1795 1796 1797 1798 1799 1800 1801 |
# File 'numeric.c', line 1794
static VALUE
int_odd_p(VALUE num)
{
if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
return Qtrue;
}
return Qfalse;
}
|
#ord ⇒ Integer
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.
1900 1901 1902 |
# File 'numeric.c', line 1900 static VALUE int_ord(num) VALUE num; |
#pred ⇒ Integer
Returns the Integer
equal to int - 1.
1.pred #=> 0
(-1).pred #=> -2
1852 1853 1854 1855 1856 1857 1858 1859 1860 |
# File 'numeric.c', line 1852
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));
}
|
#to_i ⇒ Integer #to_int ⇒ Integer #floor ⇒ Integer #ceil ⇒ Integer #round ⇒ Integer #truncate ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1766 1767 1768 |
# File 'numeric.c', line 1766 static VALUE int_to_i(num) VALUE num; |
#next ⇒ Integer #succ ⇒ Integer
Returns the Integer
equal to int + 1.
1.next #=> 2
(-1).next #=> 0
1831 1832 1833 |
# File 'numeric.c', line 1831 static VALUE int_succ(num) VALUE num; |
#times {|i| ... } ⇒ Integer
Iterates block int times, passing in values from zero to int - 1.
5.times do |i|
print i, " "
end
produces:
0 1 2 3 4
2926 2927 2928 |
# File 'numeric.c', line 2926 static VALUE int_dotimes(num) VALUE num; |
#to_i ⇒ Integer #to_int ⇒ Integer #floor ⇒ Integer #ceil ⇒ Integer #round ⇒ Integer #truncate ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1766 1767 1768 |
# File 'numeric.c', line 1766 static VALUE int_to_i(num) VALUE num; |
#to_i ⇒ Integer #to_int ⇒ Integer #floor ⇒ Integer #ceil ⇒ Integer #round ⇒ Integer #truncate ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1766 1767 1768 |
# File 'numeric.c', line 1766 static VALUE int_to_i(num) VALUE num; |
#to_i ⇒ Integer #to_int ⇒ Integer #floor ⇒ Integer #ceil ⇒ Integer #round ⇒ Integer #truncate ⇒ Integer
As int is already an Integer
, all these methods simply return the receiver.
1766 1767 1768 |
# File 'numeric.c', line 1766 static VALUE int_to_i(num) VALUE num; |
#upto(limit) {|i| ... } ⇒ Integer
Iterates block, passing in integer values from int up to and including limit.
5.upto(10) { |i| print i, " " }
produces:
5 6 7 8 9 10
2843 2844 2845 |
# File 'numeric.c', line 2843 static VALUE int_upto(from, to) VALUE from, to; |