Skip to content

Instantly share code, notes, and snippets.

@phorsfall
Created May 8, 2009 16:18
Show Gist options
  • Save phorsfall/108862 to your computer and use it in GitHub Desktop.
Save phorsfall/108862 to your computer and use it in GitHub Desktop.
class Version < Struct.new(:version_string)
include Comparable
def [](version_string)
new(version_string)
end
def to_a
version_string.split(".").map { |s| s.to_i }
end
def <=>(other)
self.to_a <=> other.to_a
end
end
def bigger?(s1, s2)
Version[s1] > Version[s2]
end
require 'test/unit'
class VersionTest < Test::Unit::TestCase
def test_major_versions
assert Version["2"] > Version["1"]
assert Version["3"] < Version["4"]
end
def test_major_minor_versions
assert Version["1.6"] > Version["1.5"]
assert Version["1.9"] < Version["1.91"]
end
def test_major_minor_against_major
assert Version["1.6"] > Version["1"]
assert Version["2"] < Version["2.1"]
end
def test_long_version_numbers
assert Version["2.3.4.1.1.1.1"] > Version["1.9.4.5.56"]
end
def test_equality
assert_equal Version["2.0"], Version["2.0"]
assert_not_equal Version["2.0"], Version["2.0.0"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment