Skip to content

Instantly share code, notes, and snippets.

@paytonrules
Created April 29, 2013 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paytonrules/5484418 to your computer and use it in GitHub Desktop.
Save paytonrules/5484418 to your computer and use it in GitHub Desktop.
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