2

以下のコードを実行すると、次のエラーが表示されます

/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423: /Users/Castillo/.rvm/rubies/ruby-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 内のブロック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:infu_each_src_dest' from /Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/ fileutils.rb:391:in cp' from learn3.rb:65:inexecute' from learn3.rb:83:in block in execute' from learn3.rb:83:ineach' from learn3.rb:83:in 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
4

1 に答える 1

1

行のソースファイル名にタイプミスがありますcmds.add_command(CopyFile.new('file.txt', 'file2.txt'))

この行を次のように変更しますcmds.add_command(CopyFile.new('file1.txt', 'file2.txt'))

于 2012-07-19T05:44:54.150 に答える