# rubinius/rubinius

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Merge remote-tracking branch 'davorb/master'

commit 75cb37a6b88980253714feeb35588fe2106f730c 2 parents 79e812a + b67df1f
brixen authored
1  AUTHORS
 @@ -61,6 +61,7 @@ - David Waite: { irc: ddubs } - David Whittington: { irc: djwhitt } - David Yip: +- Davor Babic: { irc: davorb, github: davorb, email: davor@davor.se } - Defn: - Dirkjan Bussink: { irc: dbussink } - Drew Olson:
77 kernel/common/integer.rb
 @@ -147,5 +147,82 @@ def downto(val) end self end + + # + # In an integer, the value _is_ the numerator of its rational equivalent. + # Therefore, this method returns +self+. + # + def numerator + self + end + + # + # In an integer, the denominator is 1. Therefore, this method returns 1. + # + def denominator + 1 + end + + # + # Returns a Rational representation of this integer. + # + def to_r + Rational(self, 1) + 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. + # + def gcd(other) + raise TypeError, "Expected Integer but got #{other.class}" unless other.kind_of?(Integer) + min = self.abs + max = other.abs + while min > 0 + tmp = min + min = max % min + max = tmp + end + max + end + + # + # Returns the lowest common multiple (LCM) of the two arguments + # (+self+ and +other+). + # + # Examples: + # 6.lcm 7 # -> 42 + # 6.lcm 9 # -> 18 + # + def lcm(other) + if self.zero? or other.zero? + 0 + else + (self.div(self.gcd(other)) * other).abs + end + 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] + # + def gcdlcm(other) + gcd = self.gcd(other) + if self.zero? or other.zero? + [gcd, 0] + else + [gcd, (self.div(gcd) * other).abs] + end + end end
79 kernel/common/rational.rb
 @@ -438,85 +438,6 @@ def hash private :initialize end -class Integer - # - # In an integer, the value _is_ the numerator of its rational equivalent. - # Therefore, this method returns +self+. - # - def numerator - self - end - - # - # In an integer, the denominator is 1. Therefore, this method returns 1. - # - def denominator - 1 - end - - # - # Returns a Rational representation of this integer. - # - def to_r - Rational(self, 1) - 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. - # - def gcd(other) - raise TypeError, "Expected Integer but got #{other.class}" unless other.kind_of?(Integer) - min = self.abs - max = other.abs - while min > 0 - tmp = min - min = max % min - max = tmp - end - max - end - - # - # Returns the lowest common multiple (LCM) of the two arguments - # (+self+ and +other+). - # - # Examples: - # 6.lcm 7 # -> 42 - # 6.lcm 9 # -> 18 - # - def lcm(other) - if self.zero? or other.zero? - 0 - else - (self.div(self.gcd(other)) * other).abs - end - 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] - # - def gcdlcm(other) - gcd = self.gcd(other) - if self.zero? or other.zero? - [gcd, 0] - else - [gcd, (self.div(gcd) * other).abs] - end - end -end - class Fixnum remove_method :quo