Created
June 27, 2013 04:00
-
-
Save suan/5873845 to your computer and use it in GitHub Desktop.
Compare 2 equal-length version numbers in 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 VersionNumber | |
include Comparable | |
attr_reader :version_parts | |
def initialize version_string | |
@version_parts = version_string.split(/[a-zA-Z]/).first.split('.').map(&:to_i) | |
end | |
def <=> other | |
@version_parts.each_with_index do |version_part, i| | |
if (comparison = version_part <=> other.version_parts[i]) != 0 | |
return comparison | |
end | |
end | |
return 0 | |
end | |
end | |
puts "works!" if VersionNumber.new('4.2.12') < VersionNumber.new('4.2.123r13') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment