Add double dispatch to Integer
- C
- D
- E
- F
- G
- I
- L
- N
- O
- P
- R
- S
- T
- U
- rb_mPrecision START:includes
Source: show
# File lib/mathn.rb, line 37 37: def Integer.from_prime_division(pd) 38: value = 1 39: for prime, index in pd 40: value *= prime**index 41: end 42: value 43: end
Convert obj to an Integer.
Source: show
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.
Source: show
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"
Source: show
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.
Source: show
# File lib/rational.rb, line 449 449: def denominator 450: 1 451: end
Source: show
# File lib/complex.rb, line 422 422: def denominator() 1 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!
Source: show
static VALUE
int_downto(from, to)
VALUE from, to;
{
RETURN_ENUMERATOR(from, 1, &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;
}
Returns true if int is an even number.
Source: show
static VALUE
int_even_p(VALUE num)
{
if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
return Qtrue;
}
return Qfalse;
}
As int is already an Integer, all these methods simply return the receiver.
Source: show
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
Source: show
# File lib/complex.rb, line 424 424: def gcd(other) 425: min = self.abs 426: max = other.abs 427: while min > 0 428: tmp = min 429: min = max % min 430: max = tmp 431: end 432: max 433: end
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.
Source: show
# File lib/rational.rb, line 470 470: def gcd(other) 471: min = self.abs 472: max = other.abs 473: while min > 0 474: tmp = min 475: min = max % min 476: max = tmp 477: end 478: max 479: end
Source: show
# File lib/mathn.rb, line 19 19: def gcd2(int) 20: a = self.abs 21: b = int.abs 22: a, b = b, a if a < b 23: 24: pd_a = a.prime_division 25: pd_b = b.prime_division 26: 27: gcd = 1 28: for pair in pd_a 29: as = pd_b.assoc(pair[0]) 30: if as 31: gcd *= as[0] ** [as[1], pair[1]].min 32: end 33: end 34: return gcd 35: 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]
Source: show
# File lib/rational.rb, line 505 505: def gcdlcm(other) 506: gcd = self.gcd(other) 507: if self.zero? or other.zero? 508: [gcd, 0] 509: else 510: [gcd, (self.div(gcd) * other).abs] 511: end 512: end
Always returns true.
Source: show
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
Source: show
# File lib/rational.rb, line 489 489: def lcm(other) 490: if self.zero? or other.zero? 491: 0 492: else 493: (self.div(self.gcd(other)) * other).abs 494: end 495: end
Source: show
# File lib/complex.rb, line 435 435: def lcm(other) 436: if self.zero? or other.zero? 437: 0 438: else 439: (self.div(self.gcd(other)) * other).abs 440: end 441: end
Returns the Integer equal to int + 1.
1.next #=> 2 (-1).next #=> 0
Source: show
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.
Source: show
# File lib/rational.rb, line 442 442: def numerator 443: self 444: end
Source: show
# File lib/complex.rb, line 421 421: def numerator() self end
Returns true if int is an odd number.
Source: show
static VALUE
int_odd_p(VALUE num)
{
if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
return Qtrue;
}
return Qfalse;
}
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.
Source: show
static VALUE
int_ord(num)
VALUE num;
{
return num;
}
Returns the Integer equal to int - 1.
1.pred #=> 0 (-1).pred #=> -2
Source: show
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));
}
Source: show
# File lib/mathn.rb, line 45 45: def prime_division 46: raise ZeroDivisionError if self == 0 47: ps = Prime.new 48: value = self 49: pv = [] 50: for prime in ps 51: count = 0 52: while (value1, mod = value.divmod(prime) 53: mod) == 0 54: value = value1 55: count += 1 56: end 57: if count != 0 58: pv.push [prime, count] 59: end 60: break if prime * prime >= value 61: end 62: if value > 1 63: pv.push [value, 1] 64: end 65: return pv 66: end
As int is already an Integer, all these methods simply return the receiver.
Source: show
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
Returns the Integer equal to int + 1.
1.next #=> 2 (-1).next #=> 0
Source: show
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
Source: show
static VALUE
int_dotimes(num)
VALUE num;
{
RETURN_ENUMERATOR(num, 0, 0);
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;
}
Source: show
# File ext/openssl/lib/openssl/bn.rb, line 31 31: def to_bn 32: OpenSSL::BN::new(self) 33: end
As int is already an Integer, all these methods simply return the receiver.
Source: show
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
As int is already an Integer, all these methods simply return the receiver.
Source: show
static VALUE
int_to_i(num)
VALUE num;
{
return num;
}
Returns a Rational representation of this integer.
Source: show
# File lib/rational.rb, line 456 456: def to_r 457: Rational(self, 1) 458: end
Source: show
# File lib/yaml/rubytypes.rb, line 358 358: def to_yaml( opts = {} ) 359: YAML::quick_emit( nil, opts ) do |out| 360: out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain ) 361: end 362: end
As int is already an Integer, all these methods simply return the receiver.
Source: show
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
Source: show
static VALUE
int_upto(from, to)
VALUE from, to;
{
RETURN_ENUMERATOR(from, 1, &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;
}