Got any suggestion on how to improve this Filecopy code snippet below?
$files.each_with_progress do | val |
$files.each do |element|
FileUtils.cp("#{$old_home}/#{$sName}", “#{Dir.pwd}/#{element}”)
end
end
Some guidelines to the variables in the code:
The ‘$files’ variable contains an array of destinations(paths), about
139 different paths at the moment.
$old_home and $sName is the sourcepath and sourcefilename of the file to
be copyed.
Basically, the code replaces one specific file with 139 other files with
the same name and extension in 139 different location. Or you could say,
it updates 139 files with copy/replace.
In my point of view, it seems like the code is trying to copy 139 files
at the same time to 139 different location withoutcompleting one
“copyaction” at the time before copying another file. Wouldn’t it be
more efficient to wait for one file to be completely replaced before
starting another “copyaction”. Am I wrong? Got any suggestion on how to
improve this code?
On Thu, Nov 8, 2012 at 11:08 AM, Peter H. < [email protected]> wrote:
This is the code you posted
$files.each_with_progress do | val |
$files.each do |element|
FileUtils.cp("#{$old_home}/#{$sName}", “#{Dir.pwd}/#{element}”)
end
end
I do especially wonder what val is used for and why there seem to be two
iterations through $files - this is O(n*n).
Your description looks contradictory to me: on one hand you say that
$files
holds destinations and then you say that one file is replaced by 139
files
(which would mean $files contain sources).
To copy a file into destinations
src = File.join old_home, sName
$files.each do |dst|
FileUtils.cp src, dst
end
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.