在Ruby中,如何在复制之前检查文件是否比目标更新?
我在Ruby-doc.org找到了FileUtils.uptodate?
,但它似乎无法正常工作。
运行时出现此错误消息:
/Users/bprov/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/fileutils.rb:148:in
uptodate?': undefined method
每个&#39; for&#34; / Users / bprov / folder2 / homecontroller&#34;:String(NoMethodError) 来自Test_Copy.rb:11:block in <main>' from Test_Copy.rb:6:in
foreach&#39; 来自Test_Copy.rb:6:在`&#39;
我有类似的东西:
require 'fileutils'
$sourcepath = "/Users/bprov/railsbridge"
$destinationpath = "/Users/bprov/folder2"
Dir.foreach($sourcepath) do |file|
if (file =~ /^\./ )
# Do nothing
else
puts file
if FileUtils.uptodate?($sourcepath + "/" + file, $destinationpath + "/" + file)
# Do nothing
else
# If newer
FileUtils.cp($sourcepath + "/" + file, $destinationpath + "/" + file)
end
end
end
答案 0 :(得分:4)
所以,只需正确使用它:
FileUtils.uptodate?($sourcepath + "/" + file, [$destinationpath + "/" + file])