这里的要点是用于浏览数组docfiles
和返回两个阵列(temporary_file_paths
和temporary_file_names
)。我该如何改进这个ruby代码和我对Hashes的使用?
我决定返回一个哈希,但我觉得我可以摆脱2个临时数组,但我不知道怎么...
def self.foobar docfiles
temporary_information = Hash.new
temporary_file_paths = []
temporary_file_names = []
docfiles.each do |docfile|
if File.exist? docfile.path
temporary_file_paths << "new_path"
temporary_file_names << "something_else"
end
end
temporary_information[:file_paths] = temporary_file_paths
temporary_information[:file_names] = temporary_file_names
return temporary_information
end
为什么不喜欢使用散列而不是对象?在这种情况下,你认为在你的答案的第二部分做你所描述的内容会是多么的矫枉过正? – marcgg 2009-12-21 16:23:50
,我会再次为您提供双倍的价值回报,我不知道该怎么做!这很酷,甚至认为人们可能会阅读代码时感到困惑,我不知道...... – marcgg 2009-12-21 16:25:01
使用Hashes或不是真的取决于真实的环境。如果您需要对提取路径等单个值进行多个细化,请计算“其他”,然后对象更加灵活。我只是想提供一套全面的解决方案。 – 2009-12-21 16:37:28