Ruby-FileUtils.cp\r无法覆盖

Ruby-FileUtils.cp\r无法覆盖,ruby,Ruby,我想把目录复制到另一个目录。(如果存在,则覆盖) 所以在我的循环中 FileUtils.cp_r(src, dst, :remove_destination => true) 但我遇到了错误 /home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1550:in `block in fu_each_src_dest': same file: 273 and /todpop/todpop_data/word

我想把目录复制到另一个目录。(如果存在,则覆盖) 所以在我的循环中

FileUtils.cp_r(src, dst, :remove_destination => true)
但我遇到了错误

/home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1550:in `block in fu_each_src_dest': same file: 273 and /todpop/todpop_data/word/image/273 (ArgumentError)
from /home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1565:in `fu_each_src_dest0'
from /home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1549:in `fu_each_src_dest'
from /home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:434:in `cp_r'
from copy.rb:16:in `block (2 levels) in <main>'
from copy.rb:15:in `each'
from copy.rb:15:in `block in <main>'
from copy.rb:8:in `each'
from copy.rb:8:in `<main>'
/home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1550:in'block in fu_each_src_dest':相同的文件:273和/todpop/todpop_data/word/image/273(ArgumentError)
from/home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1565:in'fu_each_src_dest0'
from/home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:1549:in'fu_each_src_dest'
from/home/deployer/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:434:in'cp\r'
from copy.rb:16:in'block(2层)in'
摘自副本。rb:15:in'each'
从copy.rb:15:in'block in'
摘自copy.rb:8:in'each'
从copy.rb:8:in`'

尽管设置了:remove\u destination,但为什么会发生此错误?T^T

看起来您正试图将文件
src
移动到完全相同的位置
dst
,并按功能返回错误。

看起来您正试图将文件
src
移动到完全相同的位置
dst
,并按功能返回错误。

以下是一些自行调试的方法。在您的帖子中,我可以看到行“block In fu_each_src_dest”:相同的文件:。现在我来看看链接。代码如下:

# File lib/fileutils.rb, line 1512
  def fu_each_src_dest(src, dest)   #:nodoc:
    fu_each_src_dest0(src, dest) do |s, d|
      raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
      yield s, d, File.stat(s)
    end
  end

现在我相信您将了解错误产生的原因。

这里有一些自己调试的方法。在您的帖子中,我可以看到行“block In fu_each_src_dest”:相同的文件:。现在我来看看链接。代码如下:

# File lib/fileutils.rb, line 1512
  def fu_each_src_dest(src, dest)   #:nodoc:
    fu_each_src_dest0(src, dest) do |s, d|
      raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
      yield s, d, File.stat(s)
    end
  end

现在我相信你会明白为什么会出现错误。

给出完整的错误堆栈第8行中出现了哪行代码?给出完整的错误堆栈第8行中出现了哪行代码?那么确切的特征是什么?@ArupRakshit,他只是说
FileUtils
被设计为在
src
dst
相同时返回错误。它应该是这样工作的。OP应该只使用一个guard子句来避免在
src
dst
相同时执行
FileUtils.cp\u r
。那么确切的功能是什么呢?@ArupRakshit,他的意思是
FileUtils
被设计为在
src
dst
相同时返回错误。它应该是这样工作的。当
src
dst
相同时,OP应该只使用一个guard子句来避免执行
FileUtils.cp\r