Ruby 无法运行FileUtils.copy,因为I';当我需要';fileutils.rb';

Ruby 无法运行FileUtils.copy,因为I';当我需要';fileutils.rb';,ruby,fileutils,Ruby,Fileutils,运行下面的代码时,我收到以下错误 /Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:instat”:没有这样的文件或目录-file.txt(Errno::enoint) from/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:inblock in fu_each_src_dest' fro

运行下面的代码时,我收到以下错误

/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in
stat”:没有这样的文件或目录-file.txt(Errno::enoint)
from/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in
block in fu_each_src_dest' from/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1439:in
fu\u each\u src\u dest0'
from/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1421:in
fu\u each\u src\u dest' from/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:391:in
cp'
从learn3.rb:65:in
execute' 来自learn3.rb:83:in
block in execute'
来自learn3.rb:83:in
每个' 来自learn3.rb:83:in
execute'
来自learn3.rb:103:in
'

几周前,我在安装BREW时遇到了一些问题。在某种程度上,这可能是问题的原因吗

这是我的密码


需要'fileutils.rb'
类命令
#允许我们阅读描述
属性读取器(:说明)
#使用description命令初始化类实例
def初始化(说明)
@描述=描述
结束
def执行
结束
结束
类CreateFile<命令
#从命令类继承
#以两个参数开头
def初始化(路径、内容)
#已从父类初始化描述
超级(“创建文件:#{path}”)
@路径=路径
@内容=内容
结束
def执行
#以可写方式打开文件
f=文件。打开(@path,“w”)
#将内容写入文件
f、 写入(@contents)
#关闭文件
f、 接近
结束
结束
类DeleteFile<命令
#从命令类继承
def初始化(路径)
#使用路径初始化
超级(“删除文件:#{path}”)
#从命令类继承描述
@路径=路径
结束
def执行
#删除该文件
File.delete(@path)
结束
结束
类CopyFile<命令
def初始化(源、目标)
超级(“将文件:#{source}复制到#{target}”)
@来源=来源
@目标=目标
结束
def执行
cp(@source,@target)
结束
结束
类CompositeCommand<命令
#继承描述并执行
def初始化
@命令=[]
结束
def add_命令(cmd)

@命令您在cmds.add\u命令行的源文件名中有一个输入错误(CopyFile.new('file.txt','file2.txt'))


将此行更改为
cmds.add_命令(CopyFile.new('file1.txt','file2.txt'))

,而否决投票的原因是?老实说,我不确定。。。我正在回顾我的SO历史,注意到我对此投了反对票。我真的不知道为什么我会这么做,但我已经删除了它,并给了你一个+1:d谢谢。顺便说一句,它解决了你的问题吗?哦,没有。我的问题是Windows->FileUtils,Ubuntu->FileUtils。这就解决了问题。谢谢
require 'fileutils.rb'

class Command
  # Allows us to read the description
  attr_reader(:description)

  # Initializes the class instance with the description command
  def initialize(description)
    @description = description
  end

  def execute
  end
end



class CreateFile < Command
  # Inherits from the Command class
  # Initialzied with two arguments 
  def initialize(path, contents)
    # Initialized description from parent class
    super("Create file: #{path}")
    @path = path
    @contents = contents
  end

  def execute
    # Open the file as writable
    f = File.open(@path, "w")
    # Write the contents to the file
    f.write(@contents)
    # Close the file
    f.close
  end
end



class DeleteFile < Command
  # Inherits from the Command class
  def initialize(path)
    # Initialize with the path
    super("Delete file: #{path}")
    # Inherits the description from Command class
    @path = path
  end

  def execute
    # Delete the file
    File.delete(@path)
  end
end



class CopyFile < Command
  def initialize(source, target)
    super("Copy file: #{source} to #{target}")
    @source = source
    @target = target
  end

  def execute
    FileUtils.cp(@source, @target)
  end
end



class CompositeCommand < Command
  # Inherits description and execute
  def initialize
    @commands = []
  end

  def add_command(cmd)
    @commands << cmd
  end

  def execute
    # Execute each command in the @commands array
    @commands.each { |cmd| cmd.execute }
  end

  def description
    description = ''
    # Each description neatly printed on its own line
    @commands.each { |cmd| description += cmd.description + "\n" }
    # Return all the descriptions 
    description
  end
end


cmds = CompositeCommand.new
puts cmds.description
cmds.add_command(CreateFile.new('file1.txt', "hello world\n"))
cmds.add_command(CopyFile.new('file.txt', 'file2.txt'))
cmds.add_command(DeleteFile.new('file1.txt'))
puts cmds.description
puts "Now executing all the commands"
cmds.execute
puts cmds.description