blog.robotex.de

Ruby: Line based file access

Counting lines

I did find some fancy examples, but I wanted to have it look nice, so I came up with this:

File.new('some_file.txt', "r").lines.count

Access a specific line

File.open('some_file.txt', "r") do |file|
  nth_line_content = file.lines.entries[n]
end


Back