Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master', remote-tracking branch 'upstream/master'
- Loading branch information
Showing
14 changed files
with
99 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,4 +148,3 @@ def downto(val) | |
self | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,81 @@ | ||
class Integer | ||
alias_method :magnitude, :abs | ||
|
||
# | ||
# 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) | ||
raise TypeError, "Expected Integer but got #{other.class}" unless other.kind_of?(Integer) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,5 +323,11 @@ def to_a | |
super | ||
end | ||
|
||
def max &a | ||
return super(&a) if block_given? | ||
return nil if @end < @begin | ||
@end | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
fails:String#upto doesn't call block with self even if self is less than stop but stop length is less than self length | ||
Empty file.
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters