Here's a brief idiomatic way to do this, which correctly opens the file for reading and closes it later.
File.open('path.txt', &:gets)
If you want an empty file to throw an exception, use this instead.
File.open('path.txt', &:readline)
In addition, there is a quick and dirty implementation of the head that will work for your purposes and in many other cases when you want to read a few more lines.
Blake Taylor Jan 18 '11 at 17:16 2011-01-18 17:16
source share