Skip to content

Instantly share code, notes, and snippets.

@leepa
Created November 19, 2010 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leepa/706845 to your computer and use it in GitHub Desktop.
Save leepa/706845 to your computer and use it in GitHub Desktop.
def normalize_version version, sep, max_width
version_split = version.split(sep)
output = ""
for s in version_split
output << sprintf('%' + max_width.to_s + 's', s)
end
output
end
v1 = '1.4.1'
v2 = '1.5'
v1_s = normalize_version v1, '.', 4
v2_s = normalize_version v2, '.', 4
cmp = v1_s <=> v2_s
# If cmp < 0 then v1 < v2
# If cmp > 0 then v2 > v1
# If cmp = 0 then v1 == v2
puts "#{v1} #{(cmp < 0) ? "<" : cmp > 0 ? ">" : "=="} #{v2}"
v1 = '1.9.2-p149'
v2 = '1.9.2-p150'
cmp = v1_s <=> v2_s
# If cmp < 0 then v1 < v2
# If cmp > 0 then v2 > v1
# If cmp = 0 then v1 == v2
puts "#{v1} #{(cmp < 0) ? "<" : cmp > 0 ? ">" : "=="} #{v2}"
v1 = '1.8.9-p149'
v2 = '1.9.2-p150'
cmp = v1_s <=> v2_s
# If cmp < 0 then v1 < v2
# If cmp > 0 then v2 > v1
# If cmp = 0 then v1 == v2
puts "#{v1} #{(cmp < 0) ? "<" : cmp > 0 ? ">" : "=="} #{v2}"
v1 = 'R14B'
v2 = 'R15C'
cmp = v1_s <=> v2_s
# If cmp < 0 then v1 < v2
# If cmp > 0 then v2 > v1
# If cmp = 0 then v1 == v2
puts "#{v1} #{(cmp < 0) ? "<" : cmp > 0 ? ">" : "=="} #{v2}"
v1 = 'R15C'
v2 = 'R14B'
cmp = v1_s <=> v2_s
# If cmp < 0 then v1 < v2
# If cmp > 0 then v2 > v1
# If cmp = 0 then v1 == v2
puts "#{v1} #{(cmp < 0) ? "<" : cmp > 0 ? ">" : "=="} #{v2}"
1.4.1 < 1.5
1.9.2-p149 < 1.9.2-p150
1.8.9-p149 < 1.9.2-p150
R14B < R15C
R15C < R14B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment