| Class | Integer |
| In: |
numeric.c
lib/rational.rb |
| Parent: | Numeric |
Convert obj to an Integer.
/*
* call-seq:
* Integer.induced_from(obj) => fixnum, bignum
*
* Convert <code>obj</code> to an Integer.
*/
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));
}
}
As int is already an Integer, all these methods simply return the receiver.
/*
* call-seq:
* int.to_i => int
* int.to_int => int
* int.floor => int
* int.ceil => int
* int.round => int
* int.truncate => int
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
*/
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
Returns a string containing the ASCII character represented by the receiver’s value.
65.chr #=> "A" ?a.chr #=> "a" 230.chr #=> "\346"
/*
* call-seq:
* int.chr => string
*
* Returns a string containing the ASCII character represented by the
* receiver's value.
*
* 65.chr #=> "A"
* ?a.chr #=> "a"
* 230.chr #=> "\346"
*/
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);
}
In an integer, the denominator is 1. Therefore, this method returns 1.
# File lib/rational.rb, line 417
417: def denominator
418: 1
419: end
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!
/*
* call-seq:
* int.downto(limit) {|i| block } => int
*
* Iterates <em>block</em>, passing decreasing values from <i>int</i>
* down to and including <i>limit</i>.
*
* 5.downto(1) { |n| print n, ".. " }
* print " Liftoff!\n"
*
* <em>produces:</em>
*
* 5.. 4.. 3.. 2.. 1.. Liftoff!
*/
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));
}
}
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;
}
As int is already an Integer, all these methods simply return the receiver.
/*
* call-seq:
* int.to_i => int
* int.to_int => int
* int.floor => int
* int.ceil => int
* int.round => int
* int.truncate => int
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
*/
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
Returns the greatest common denominator of the two numbers (self and n).
Examples:
72.gcd 168 # -> 24 19.gcd 36 # -> 1
The result is positive, no matter the sign of the arguments.
# File lib/rational.rb, line 438
438: def gcd(n)
439: m = self.abs
440: n = n.abs
441:
442: return n if m == 0
443: return m if n == 0
444:
445: b = 0
446: while n[0] == 0 && m[0] == 0
447: b += 1; n >>= 1; m >>= 1
448: end
449: m >>= 1 while m[0] == 0
450: n >>= 1 while n[0] == 0
451: while m != n
452: m, n = n, m if n > m
453: m -= n; m >>= 1 while m[0] == 0
454: end
455: m << b
456: end
# File lib/rational.rb, line 458
458: def gcd2(int)
459: a = self.abs
460: b = int.abs
461:
462: a, b = b, a if a < b
463:
464: while b != 0
465: void, a = a.divmod(b)
466: a, b = b, a
467: end
468: return a
469: end
Returns the GCD and the LCM (see gcd and lcm) of the two arguments (self and other). This is more efficient than calculating them separately.
Example:
6.gcdlcm 9 # -> [3, 18]
# File lib/rational.rb, line 495
495: def gcdlcm(other)
496: gcd = self.gcd(other)
497: if self.zero? or other.zero?
498: [gcd, 0]
499: else
500: [gcd, (self.div(gcd) * other).abs]
501: end
502: end
Always returns true.
/*
* call-seq:
* int.integer? -> true
*
* Always returns <code>true</code>.
*/
static VALUE
int_int_p(num)
VALUE num;
{
return Qtrue;
}
Returns the lowest common multiple (LCM) of the two arguments (self and other).
Examples:
6.lcm 7 # -> 42 6.lcm 9 # -> 18
# File lib/rational.rb, line 479
479: def lcm(other)
480: if self.zero? or other.zero?
481: 0
482: else
483: (self.div(self.gcd(other)) * other).abs
484: end
485: end
Returns the Integer equal to int + 1.
1.next #=> 2 (-1).next #=> 0
/*
* call-seq:
* int.next => integer
* int.succ => integer
*
* Returns the <code>Integer</code> equal to <i>int</i> + 1.
*
* 1.next #=> 2
* (-1).next #=> 0
*/
static VALUE
int_succ(num)
VALUE num;
{
if (FIXNUM_P(num)) {
long i = FIX2LONG(num) + 1;
return LONG2NUM(i);
}
return rb_funcall(num, '+', 1, INT2FIX(1));
}
In an integer, the value is the numerator of its rational equivalent. Therefore, this method returns self.
# File lib/rational.rb, line 410
410: def numerator
411: self
412: end
As int is already an Integer, all these methods simply return the receiver.
/*
* call-seq:
* int.to_i => int
* int.to_int => int
* int.floor => int
* int.ceil => int
* int.round => int
* int.truncate => int
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
*/
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
Returns the Integer equal to int + 1.
1.next #=> 2 (-1).next #=> 0
/*
* call-seq:
* int.next => integer
* int.succ => integer
*
* Returns the <code>Integer</code> equal to <i>int</i> + 1.
*
* 1.next #=> 2
* (-1).next #=> 0
*/
static VALUE
int_succ(num)
VALUE num;
{
if (FIXNUM_P(num)) {
long i = FIX2LONG(num) + 1;
return LONG2NUM(i);
}
return rb_funcall(num, '+', 1, INT2FIX(1));
}
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
/*
* call-seq:
* int.times {|i| block } => int
*
* Iterates block <i>int</i> times, passing in values from zero to
* <i>int</i> - 1.
*
* 5.times do |i|
* print i, " "
* end
*
* <em>produces:</em>
*
* 0 1 2 3 4
*/
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));
}
}
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;
}
As int is already an Integer, all these methods simply return the receiver.
/*
* call-seq:
* int.to_i => int
* int.to_int => int
* int.floor => int
* int.ceil => int
* int.round => int
* int.truncate => int
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
*/
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
As int is already an Integer, all these methods simply return the receiver.
/*
* call-seq:
* int.to_i => int
* int.to_int => int
* int.floor => int
* int.ceil => int
* int.round => int
* int.truncate => int
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
*/
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
As int is already an Integer, all these methods simply return the receiver.
/*
* call-seq:
* int.to_i => int
* int.to_int => int
* int.floor => int
* int.ceil => int
* int.round => int
* int.truncate => int
*
* As <i>int</i> is already an <code>Integer</code>, all these
* methods simply return the receiver.
*/
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
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
/*
* call-seq:
* int.upto(limit) {|i| block } => int
*
* Iterates <em>block</em>, passing in integer values from <i>int</i>
* up to and including <i>limit</i>.
*
* 5.upto(10) { |i| print i, " " }
*
* <em>produces:</em>
*
* 5 6 7 8 9 10
*/
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));
}
}
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;
}