lundi 29 juin 2015

Ruby - Can't write to file after checking if empty

The program should write "File Empty" if the file is empty; otherwise, it should write "File Full". Here's what I have so far:

fname = "fileTest.txt"
somefile = File.open(fname, "w")

if File.readlines(somefile).grep(/monitor/).size == 0
    somefile.write("File Empty")
else
    somefile.write("File Full.")
end

somefile.close

When I run this the first time, fileText.txt is empty, so the program writes "File Empty". When I run it a second time, the program should write "File Full", but the file still reads "File Empty".

The if statement should be checking if the file is empty, but I don't it is working correctly. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire