I have a Ruby 1.8.6 script that is supposed to take a filename that contains a path, create the folders then move the file into the folders. I have an array created_paths
to keep tracked of folders created (the script will loop through lots of files). I am having a problem adding to the created_paths
array.
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 " }
When I push tmp_path
on to the created_paths
array it seems the reference to tmp_path
has been added and not the value. On the second iteration of the loop created_paths.include?(tmp_path)
is returning True. How do I get the value of tmp_path
to be stored in my array, or maybe there is a scope issue i'm missing ?
My output:
add to array: first/
size=1
first/second/
My excepted output:
add to array: first/
add to array: first/second/
size=2
first/
first/second/