Created
April 29, 2013 20:14
-
-
Save paytonrules/5484418 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class DecimalStringComparer | |
def self.compare_decimal(a, b) | |
new(a, b).execute | |
end | |
def initialize(a, b) | |
@a = a | |
@b = b | |
end | |
def execute | |
@a, @b = @a.split("."), @b.split(".") | |
until a.empty? && b.empty? | |
result = compare(a.shift, b.shift) | |
return result if result != 0 | |
end | |
0 | |
end | |
def compare | |
case | |
when a.nil? | |
-1 | |
when b.nil? | |
1 | |
when is_integer?(a) && is_integer?(b) | |
a.to_i <=> b.to_i | |
when is_integer?(a) | |
1 | |
when is_integer?(b) | |
-1 | |
else | |
a <=> b | |
end | |
end | |
def is_integer?(n) | |
n.to_s.to_i.to_s == n.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment