Skip to content

Instantly share code, notes, and snippets.

@sczizzo
Last active December 25, 2015 00:59
Show Gist options
  • Save sczizzo/6891820 to your computer and use it in GitHub Desktop.
Save sczizzo/6891820 to your computer and use it in GitHub Desktop.
Quick version comparator
class Version
include Comparable
attr_reader :a, :b, :c, :str
def initialize str
parts = str.split('.')
raise unless parts.length == 3
@a, @b, @c = parts.map(&:to_i)
@str = str
end
def inspect ; @str end
def <=> other
if
self.a == other.a &&
self.b == other.b &&
self.c == other.c
return 0
elsif
self.a > other.a ||
(self.a == other.a && self.b > other.b) ||
(self.a == other.a && self.b == other.b && self.c > other.c)
return 1
else
return -1
end
end
end
def v str ; Version.new(str) end
# e.g. puts "hello" if v('1.2.1') > v('1.2.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment