Created
September 13, 2011 22:36
-
-
Save bgreenlee/1215379 to your computer and use it in GitHub Desktop.
Compare two version strings. #ruby
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
class VersionString < String | |
def <=>(other) | |
my_parts = self.split('.').map(&:to_i) | |
other_parts = other.split('.').map(&:to_i) | |
my_parts.each_with_index do |part, i| | |
break if i >= other_parts.length | |
next if part == other_parts[i] | |
return part <=> other_parts[i] | |
end | |
if my_parts.length > other_parts.length | |
return 1 | |
elsif other_parts.length > my_parts.length | |
return -1 | |
else | |
return 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment