用什么来存储ruby数组/散列/集中的版本号列表?

时间:2011-08-11 12:32:19

标签: ruby

我希望根据发布文件夹中的每个版本获取我的软件版本的有序列表。即/releases/7.1/releases/7.2

# 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}" }

给我我的清单

7.5
7.8
7.9
4.10
7.3
7.11
7.2
7.4
7.6
7.1
7.10

但现在我想命令它们看起来像这样: -

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

那么Symbol s?!

非常感谢。

2 个答案:

答案 0 :(得分:2)

我建议你使用versionomy gem。或者,如果您不想使用gem创建自己的类并混合使用类似的模块并实现版本比较(关于如何执行此操作有很多问题)

答案 1 :(得分:1)

为什么不在另一个数组中收集版本并在打印前对其进行排序?

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 }