主要的未定义方法'close':Ruby中的Object(NoMethodError)

Pra*_*dra 0 ruby file fclose learn-ruby-the-hard-way

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)
Run Code Online (Sandbox Code Playgroud)

程序运行正常,直到结束,但在打印第二个文件的内容后立即崩溃标题错误消息.

我使用(.class)检查了txt,txt_again并确认它们都是File对象.为什么不靠近工作?

fis*_*ben 5

您需要调用close文件对象:

txt.close
Run Code Online (Sandbox Code Playgroud)