Skip to content

Instantly share code, notes, and snippets.

@suan
Created June 27, 2013 04:00
Show Gist options
  • Save suan/5873845 to your computer and use it in GitHub Desktop.
Save suan/5873845 to your computer and use it in GitHub Desktop.
Compare 2 equal-length version numbers in Ruby
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