无法运行FileUtils.copy,因为我在需要'fileutils.rb'时收到错误

时间:2012-07-18 22:34:32

标签: ruby fileutils

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

/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in stat': No such file or directory - file.txt (Errno::ENOENT) from /Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in阻止fu_each_src_dest'     来自/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1439:in fu_each_src_dest0' from /Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1421:in fu_each_src_dest'     来自/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:391:in cp' from learn3.rb:65:in执行'     来自learn3.rb:83:in block in execute' from learn3.rb:83:in each'     来自learn3.rb:83:execute' from learn3.rb:103:in'

几周后我在安装BREW时遇到了一些问题。这可能是某种方式导致问题的原因吗?

张贴是我的代码


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

1 个答案:

答案 0 :(得分:1)

您在第cmds.add_command(CopyFile.new('file.txt', 'file2.txt'))

的源文件名中输入了拼写错误

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