Last active
December 25, 2015 00:59
-
-
Save sczizzo/6891820 to your computer and use it in GitHub Desktop.
Quick version comparator
This file contains hidden or 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 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