检查文件是否比 Ruby 中的其他文件更新?

BPr*_*rov -1 ruby file-io

在 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 methodeach' for "/Users/bprov/folder2/homecontroller":String (NoMethodError ) 来自 Test_Copy.rb:11:in block in <main>' from Test_Copy.rb:6:inforeach' 来自 Test_Copy.rb:6:in `'

我有这样的事情:

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
Run Code Online (Sandbox Code Playgroud)

zis*_*she 5

所以,只要正确使用它:

FileUtils.uptodate?($sourcepath + "/" + file, [$destinationpath + "/" + file])
Run Code Online (Sandbox Code Playgroud)