folder_to_analyze = ARGV.first
folder_path = File.join(Dir.pwd, folder_to_analyze)
unless File.directory?(folder_path)
puts "Error: #{folder_path} no es un folder valido."
exit
end
def get_csv_file_paths(path)
files = []
Dir.glob(path + '/**/*.csv').each do |f|
files << f
end
return files
end
def get_xlsx_file_path(path)
files = []
Dir.glob(path + '/**/*.xls').each do |f|
files << f
end
return files
end
files_to_process = []
files_to_process << get_csv_file_paths(folder_path)
files_to_process << get_xlsx_file_path(folder_path)
puts files_to_process[1].length # Not what I want, I want:
# puts files_to_process.length
我正在尝试用Ruby编写一个简单的脚本,使我可以从命令行调用它,例如ruby counter.rb mailing_list1
,然后它会进入文件夹并统计所有.csv和.xls文件。我打算在每个文件上操作,获取行数等等。
目前
files_to_process
数组实际上是一个嵌套数组 - 我不想要那样。我想要一个包含所有.csv和.xls文件的单一数组。由于我不知道如何从
Dir.glob
中yield,所以我将它们添加到了一个数组中并返回了该数组。如何使用单个数组来完成这个任务?