What to use to store a list of version numbers in ruby array/hash/set?
I want to get a ordered list of my software releases based on each release we have is in a releases folder. ie /releases/7.1
, /releases/7.2
, etc
# Regexp out the number from the filename
def get_version(filename)
[0,0] # obviously returns different numbers based on regx
# {:major => 0, :minor => 0} # or was thinking about a Hash?
end
versions = []
#versions = Array.new() # Is this better?
Dir.new(basedir).entries.each do |filename|
versions << get_version(filename) if not File.directory? filename
end
versions.each {|major, minor| puts "#{major}.#{minor}" }
Gives me my list
7.5
7.8
7.9
4.10
7.3
7.11
7.2
7.4
7.6
7.1
7.10
But now I want to order them to look something like this :-
4.10
7.1
7.2
7.3
7.4开发者_开发技巧
7.5
7.6
7.8
7.9
7.10 # I appreciate that these might appear below 7.1, I dont mind too much about that
7.11
And what about Symbol
s?!
Many thanks.
I suggest you to use versionomy gem. Or if you don't want to use gem create your own class and mix in comparable module and implement version comparison (there are a lot of questions on SO regarding how to do this)
Why dont you collect versions in another array and sort it before printing?
versions_array = Dir.new(basedir).entries.map do |filename|
get_version(filename) unless File.directory? filename
end
versions_array.compact!
versions = versions_array.map {|major, minor| "#{major}.#{minor}" }
puts version.sort { |a, b| a.to_f <=> b.to_f }
精彩评论