我有一個Ruby 1.8.6腳本,應該採取包含路徑的文件名,創建文件夾,然後將文件移動到文件夾。我有一個數組created_paths
來跟蹤創建的文件夾(腳本將循環大量文件)。我在添加到created_paths
陣列時遇到問題。紅寶石添加變量值到一個數組
created_paths = Array.new
file_name = "first\\second\\third.txt"
parts = file_name.split('\\')
tmp_path = ""
parts.each_with_index { |part,i|
if i == (parts.length - 1)
# copy file to new dir structure
else
tmp_path << part << "/"
if !created_paths.include?(tmp_path)
puts "add to array: #{tmp_path}"
created_paths.push(tmp_path)
# create folder
end
end
}
puts "size=#{created_paths.length}\n"
created_paths.each { |z| print z, "\n " }
當我到created_paths
陣列推tmp_path
似乎參照tmp_path
已被添加,而不是值。在循環created_paths.include?(tmp_path)
的第二次迭代返回True。我如何獲得tmp_path
的值存儲在我的數組中,或者可能存在我缺少的範圍問題?
我的輸出:
add to array: first/
size=1
first/second/
我的除外輸出:
add to array: first/
add to array: first/second/
size=2
first/
first/second/
我試圖避免Dir.mkdir一個SystemCallError如果目錄已創建。我仍然對Ruby API不熟悉,FileUtils看起來更好。感謝您的幫助。 – Mark 2010-03-09 17:00:05