我正在编写一个简单的程序,它接受一个输入字符串,将其拆分为单词并将其保存在它的内存中。有三种方法——将字符串保存到内存中、从文件加载和从 zip 存档加载。这是代码:
require 'zip'
class Storage
def initialize
@storage = ''
end
def add(string)
words = string.split ','
words.each do |word|
@storage << "#{word},"
end
end
def load_from_file(filename)
File.open filename, 'r' do |f|
f.each { |line| add line }
end
end
def load_from_zip(filename)
Zip::File.open "#{filename}.zip" do |zipfile|
zipfile.each { |entry| load_from_file entry.to_s }
end
end
end
虽然add
和load_from_file
方法按我load_from_zip
的预期完美运行,但每次我尝试运行它时都会返回以下错误:
storage.rb:39:in `initialize': No such file or directory @ rb_sysopen - test.txt (Errno::ENOENT)
尽管该文件存在于我的存档中。对于我做错的任何建议,我将不胜感激