Class: Integer
Overview
Integer
is the basis for the two concrete classes that hold whole numbers, Bignum
and Fixnum
.
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.
-
#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. -
#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?, #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.
|
# File 'numeric.c'
static VALUE
rb_int_induced_from(klass, x)
VALUE klass, x;
{
switch (TYPE(x)) {
case T_FIXNUM:
case T_BIGNUM:
return x;
case T_FLOAT:
return rb_funcall(x, id_to_i, 0);
default:
rb_raise(rb_eTypeError, "failed to convert %s into Integer",
rb_obj_classname(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.
|
# File 'numeric.c'
static VALUE
int_to_i(num)
VALUE num;
{
return 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"
|
# File 'numeric.c'
static VALUE
int_chr(num)
VALUE num;
{
char c;
long i = NUM2LONG(num);
if (i < 0 || 0xff < i)
rb_raise(rb_eRangeError, "%ld out of char range", i);
c = i;
return rb_str_new(&c, 1);
}
|
#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!
|
# File 'numeric.c'
static VALUE
int_downto(from, to)
VALUE from, to;
{
if (FIXNUM_P(from) && FIXNUM_P(to)) {
long i, end;
end = FIX2LONG(to);
for (i=FIX2LONG(from); i >= end; i--) {
rb_yield(LONG2FIX(i));
}
|
#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.
|
# File 'numeric.c'
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
|
#integer? ⇒ true
Always returns true
.
|
# File 'numeric.c'
static VALUE
int_int_p(num)
VALUE num;
{
return Qtrue;
}
|
#next ⇒ Integer #succ ⇒ Integer
Returns the Integer
equal to int + 1.
1.next #=> 2
(-1).next #=> 0
|
# File 'numeric.c'
static VALUE
int_succ(num)
VALUE num;
{
if (FIXNUM_P(num)) {
long i = FIX2LONG(num) + 1;
return LONG2NUM(i);
}
|
#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.
|
# File 'numeric.c'
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
|
#next ⇒ Integer #succ ⇒ Integer
Returns the Integer
equal to int + 1.
1.next #=> 2
(-1).next #=> 0
|
# File 'numeric.c'
static VALUE
int_succ(num)
VALUE num;
{
if (FIXNUM_P(num)) {
long i = FIX2LONG(num) + 1;
return LONG2NUM(i);
}
|
#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
|
# File 'numeric.c'
static VALUE
int_dotimes(num)
VALUE num;
{
if (FIXNUM_P(num)) {
long i, end;
end = FIX2LONG(num);
for (i=0; i<end; i++) {
rb_yield(LONG2FIX(i));
}
|
#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.
|
# File 'numeric.c'
static VALUE
int_to_i(num)
VALUE num;
{
return 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.
|
# File 'numeric.c'
static VALUE
int_to_i(num)
VALUE num;
{
return 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.
|
# File 'numeric.c'
static VALUE
int_to_i(num)
VALUE num;
{
return 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
|
# File 'numeric.c'
static VALUE
int_upto(from, to)
VALUE from, to;
{
if (FIXNUM_P(from) && FIXNUM_P(to)) {
long i, end;
end = FIX2LONG(to);
for (i = FIX2LONG(from); i <= end; i++) {
rb_yield(LONG2FIX(i));
}
|