newUserNameHere newUserNameHere - 7 months ago 81
Ruby Question

How to detect the last row in CSV (ruby)

data = IO::read(file).scrub("")
CSV.parse(data, {:col_sep => "\t", :headers => headers, :quote_char => '_'}) do |row|
# how to detect last line of CSV?

I have a giant CSV file that needs to be scrubbed. It has multiple lines that apply to one DB object. In my code I'm collecting all the lines that apply to one object before passing them off to a class that will process them.

It would be very helpful if I could detect the last line in the CSV so I could make sure the last collection gets sent.

Answer do |file|
  file.each_line do |line|
    row = CSV.parse_line(line.scrub(""), col_sep: "\t", headers: headers, quote_char: '_')

I went with this solution it does not require loading the entire CSV file prior to looping through which is helpful when dealing with large files.

By using I can call file.eof? (end of file) which lets me know when I hit the last line.