def archive_files(source, glob_mask, destination) Dir.glob(File.join(source, glob_mask)) do |file| should_copy_file = true if (block_given?) then should_copy_file = yield(file) end if (should_copy_file) then FileUtils.cp_r(file, destination) if File.file?(file) end end end
Refactorings
No refactoring yet !
Josh N
March 12, 2009, March 12, 2009 20:11, permalink
def archive_files(source, mask, destination)
Dir.glob(File.join(source, mask)) do |file|
next unless File.file?(file)
FileUtils.cp(file, destination) if (!block_given? || yield(file))
end
end
Josh N
March 12, 2009, March 12, 2009 20:13, permalink
if i understand correctly you only care about copying files (not folders)
def archive_files(source, mask, destination)
Dir.glob(source, mask) do |file|
next unless File.file?(file)
FileUtils.cp(file, destination) if (!block_given? || yield(file))
end
end
Tj Holowaychuk
March 14, 2009, March 14, 2009 21:41, permalink
FileUtils.cp_r Dir['src/*.foo'], '/some/path/'
teamco-anthill.blogspot.com
March 15, 2009, March 15, 2009 12:31, permalink
Refactored by Netbeans hints
def archive_files(source, glob_mask, destination)
Dir.glob(File.join(source, glob_mask)) do |file|
should_copy_file = true
should_copy_file = yield(file) if (block_given?)
FileUtils.cp_r(file, destination) if File.file?(file) if (should_copy_file)
end
end
Tj Holowaychuk
March 15, 2009, March 15, 2009 17:43, permalink
Whats with all these parens you guys? ... and IMO source should just be part of the glob, if not then the glob should have a default and be the last param. also no clue what the yielding of the filepath is about
def archive_files glob, destination, &block
Dir[glob] do |file|
FileUtils.cp_r file, destination if File.file? file
end
end
This works, but I feel like there should be a more idiomatic way to do this. Forgive me, my ruby-fu is weak.