To get the number of lines, you can do a couple different things.
If you are on Linux or Mac OS, take advantage of the underlying OS and ask it how many lines are in the file:
lines_in_file = `wc -l #{ path_to_file_to_read }`
wc
is extremely fast, and can tell you about lines, words and characters. -l
specifies lines.
If you want to do it in Ruby, you could use File.readlines('/path/to/file/to/read')
or File.read('/path/to/file/to/read').lines
, but be very careful. Both will read the entire file into memory, and, if that file is bigger than your available RAM you've just beaten your machine to a slow death. So, don't do that.
Instead use something like:
lines_in_file = 0
File.foreach('/path/to/file/to/read') { lines_in_file += 1 }
After running, lines_in_file
will hold the number of lines in the file. File.foreach
is VERY fast, pretty much equal to using File.readlines
and probably faster than File.read().lines
, and it only reads a line at a time so you're not filling your RAM.
If you want to know the current line number of the line you just read from a file, you can use Ruby's $.
.
You're concerned about "percentage of a file" though. A potential problem with this is lines are variable length. Depending on what you are doing with them, the line length could have a big effect on your progress meter. You might want to look at the actual length of the file and keep track of the number of characters consumed by reading each line, so your progress is based on percentage of characters, rather than percentage of lines.