0
filename = ARGV.first

txt = open filename

puts "Here's your file #{filename}:"
print txt.read

puts "Type the filename again: "
file_again = $stdin.gets.chomp

txt_again = open file_again

print txt_again.read

close(txt)
close(txt_again)

该程序运行良好直到最后,但在打印第二个文件的内容后立即崩溃并显示标题错误消息。

我再次使用 (.class) 检查了 txt、txt_,并确认它们都是 File 对象。为什么不关闭工作?

4

1 回答 1

5

您需要调用close文件对象:

txt.close
于 2015-04-15T09:13:19.780 回答