Skip to content

Instantly share code, notes, and snippets.

@jjthrash
Created June 20, 2011 14:52
Show Gist options
  • Select an option

  • Save jjthrash/1035755 to your computer and use it in GitHub Desktop.

Select an option

Save jjthrash/1035755 to your computer and use it in GitHub Desktop.
class Version
include Comparable
attr_reader :major, :feature_group, :feature, :bugfix
def initialize(version="")
version = "0.0" unless version
v = version.split(".")
@major = v[0].to_i
@feature_group = v[1].to_i
@feature = v[2].to_i
@bugfix = v[3].to_i
end
def <=>(other)
if String === other
self <=> Version.new(other)
else
return @major <=> other.major if ((@major <=> other.major) != 0)
return @feature_group <=> other.feature_group if ((@feature_group <=> other.feature_group) != 0)
return @feature <=> other.feature if ((@feature <=> other.feature) != 0)
return @bugfix <=> other.bugfix
end
end
def self.sort
self.sort!{|a,b| a <=> b}
end
def to_s
@major.to_s + "." + @feature_group.to_s + "." + @feature.to_s + "." + @bugfix.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment