Class: Integer
- Defined in:
- lib/extensions/openssl/openssl/bn.rb,
lib/framework/builtinME.rb,
lib/framework/rational18.rb,
lib/framework/rationalME.rb
Overview
Add double dispatch to Integer
Direct Known Subclasses
Instance Method Summary collapse
-
#denominator ⇒ Object
In an integer, the denominator is 1.
- #downto(to) ⇒ Object
-
#gcd(other) ⇒ Object
Returns the greatest common denominator of the two numbers (
self
andn
). -
#gcdlcm(other) ⇒ Object
Returns the GCD and the LCM (see #gcd and #lcm) of the two arguments (
self
andother
). -
#integer? ⇒ Boolean
Always returns true.
-
#lcm(other) ⇒ Object
Returns the lowest common multiple (LCM) of the two arguments (
self
andother
). -
#next ⇒ Object
Returns the Integer equal to int + 1.
-
#numerator ⇒ Object
In an integer, the value is the numerator of its rational equivalent.
- #size ⇒ Object
-
#succ ⇒ Object
Synonym for Integer#next.
- #to_bn ⇒ Object
- #to_i ⇒ Object (also: #to_int)
-
#to_r ⇒ Object
Returns a Rational representation of this integer.
- #upto(to) ⇒ Object
Methods inherited from Numeric
#abs, #div, #divmod, #floor, #modulo, #nonzero?, #remainder, #zero?
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?
Instance Method Details
#denominator ⇒ Object
In an integer, the denominator is 1. Therefore, this method returns 1.
419 420 421 |
# File 'lib/framework/rational18.rb', line 419 def denominator 1 end |
#downto(to) ⇒ Object
559 560 561 562 563 564 565 |
# File 'lib/framework/builtinME.rb', line 559 def downto(to) a = self while a >= to yield a a -= 1 end end |
#gcd(other) ⇒ Object
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.
440 441 442 443 444 445 446 447 448 449 |
# File 'lib/framework/rational18.rb', line 440 def gcd(other) min = self.abs max = other.abs while min > 0 tmp = min min = max % min max = tmp end max end |
#gcdlcm(other) ⇒ Object
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]
475 476 477 478 479 480 481 482 |
# File 'lib/framework/rational18.rb', line 475 def gcdlcm(other) gcd = self.gcd(other) if self.zero? or other.zero? [gcd, 0] else [gcd, (self.div(gcd) * other).abs] end end |
#integer? ⇒ Boolean
Always returns true
547 548 549 |
# File 'lib/framework/builtinME.rb', line 547 def integer? true end |
#lcm(other) ⇒ Object
Returns the lowest common multiple (LCM) of the two arguments (self
and other
).
Examples:
6.lcm 7 # -> 42
6.lcm 9 # -> 18
459 460 461 462 463 464 465 |
# File 'lib/framework/rational18.rb', line 459 def lcm(other) if self.zero? or other.zero? 0 else (self.div(self.gcd(other)) * other).abs end end |
#next ⇒ Object
Returns the Integer equal to int + 1
537 538 539 |
# File 'lib/framework/builtinME.rb', line 537 def next self + 1 end |
#numerator ⇒ Object
In an integer, the value is the numerator of its rational equivalent. Therefore, this method returns self
.
412 413 414 |
# File 'lib/framework/rational18.rb', line 412 def numerator self end |
#size ⇒ Object
567 568 569 |
# File 'lib/framework/builtinME.rb', line 567 def size 4 end |
#succ ⇒ Object
Synonym for Integer#next
542 543 544 |
# File 'lib/framework/builtinME.rb', line 542 def succ self + 1 end |
#to_bn ⇒ Object
31 32 33 |
# File 'lib/extensions/openssl/openssl/bn.rb', line 31 def to_bn OpenSSL::BN::new(self.to_s(16), 16) end |
#to_i ⇒ Object Also known as: to_int
530 531 532 |
# File 'lib/framework/builtinME.rb', line 530 def to_i return self end |
#to_r ⇒ Object
Returns a Rational representation of this integer.
426 427 428 |
# File 'lib/framework/rational18.rb', line 426 def to_r Rational(self, 1) end |
#upto(to) ⇒ Object
551 552 553 554 555 556 557 |
# File 'lib/framework/builtinME.rb', line 551 def upto(to) a = self while a <= to yield a a += 1 end end |