thanks for the suggestions - it is a very big file, though (many thousands of lines), and I have to do this many times in a loop. Any less resource-intensive way? Maybe a unix command that could be run by the ruby script that magically gives you last line of a file, then allows you to delete last line of file, then i can just append new to file? Best, DAN On Sep 27, 2007, at 12:31 AM, Alex Gutteridge wrote: > On 27 Sep 2007, at 12:23, blufur wrote: > >> how can my ruby program tell me how many lines are in a text file? > > IO.foreach iterates over the lines in a file which you can then > count, or IO.read reads the whole thing into a string from which > you can then count the number of lines. > >> how can i have my ruby program edit the last line of a text file >> (not add another line at the end, but get and then change the line >> at the end). > > Easiest (IMO) is to read the whole file, edit the last line and > write back out. E.g: > > lines = IO.read('file').split > lines[-1] = 'edit' > puts lines > >> Thanks for any suggestions! > > Alex Gutteridge > > Bioinformatics Center > Kyoto University > > >