EX15 and the close() command

I have typed in the text for the program and compiled it. Having done that I have started on the Study Drills, the last of which states:

“Have your script also call close() on the txt and txt_again variables.” I have tried that but I get an error. I have tried a number of different ways of closing the first file before the second opens - I have added the script and my comment below. Anyone any ideas?

filename = ARGV.first

txt = open(filename)

puts “Here’s your file #{filename}:”
print txt.read

close() #This does not work like this or including the filename (ex15_sample.txt) or the word “filename” in the brackets

print "\n Type the filename again: "
file_again = $stdin.gets.chomp

txt_again = open(file_again)

print txt_again.read

You need to call .close() on something…

Thanks - the text book instruction direct me to close the txt file before opening it again and then closing it. If I include either the file name or “filename” in the parenthesis that does not work either.

You need to close the file handle. Try txt.close.

OK - thank you I will give that a try

A free service run by Zed A. Shaw for learncodethehardway.org.