【发布时间】:2025-12-05 09:45:01
【问题描述】:
我正在编写一个简单的程序,它接受一个输入字符串,将其拆分为单词并将其保存在它的内存中。有三种方法——将字符串保存到内存中、从文件加载和从 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)
虽然该文件存在于我的存档中。对于我做错的任何建议,我将不胜感激
【问题讨论】: