gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:33 25 4
gpt4 key购买 nike

<分区>

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

/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in stat': 没有这样的文件或目录 - 文件。 txt (Errno::ENOENT)
来自/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/fileutils.rb:1423:in
block 在 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'
来自/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'
来自 learn3.rb:65:in
执行' 来自 learn3.rb:83:in block in execute'
来自 learn3.rb:83:in
each' 来自 learn3.rb:83:in execute'
来自 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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com