Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote-tracking branch 'davorb/master'

  • Loading branch information...
commit 75cb37a6b88980253714feeb35588fe2106f730c 2 parents 79e812a + b67df1f
@brixen brixen authored
Showing with 78 additions and 79 deletions.
  1. +1 −0  AUTHORS
  2. +77 −0 kernel/common/integer.rb
  3. +0 −79 kernel/common/rational.rb
View
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:
View
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 <em>greatest common denominator</em> 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 <em>lowest common multiple</em> (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
View
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 <em>greatest common denominator</em> 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 <em>lowest common multiple</em> (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
Please sign in to comment.
Something went wrong with that request. Please try again.