Cannot run FileUtils.copy because I'm receiving an error when I require 'fileutils.rb' [closed]

StackOverflow https://stackoverflow.com/questions/11551362

  •  21-06-2021
  •  | 
  •  

Question

I am receiving the following error when running my code below

/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:inblock 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_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'

I had some issues installing BREW a couple weeks back. Could this be the cause of the problem in some way?

POSTED IS MY CODE


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
Was it helpful?

Solution

You have a typo in source file name at line cmds.add_command(CopyFile.new('file.txt', 'file2.txt'))

Changes this line to cmds.add_command(CopyFile.new('file1.txt', 'file2.txt'))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top