0

In a situation like so:

FasterCSV.parse(uploaded_file) do |row|
  @rows[i] = row
  i += 1
  break row if i == 10
end

Does FasterCSV read the entire file, or just the first 10 rows? I want to display the first couple lines of a CSV file, but don't want to store the entire file in memory. Is this possible?

Asherlc
  • 1,111
  • 2
  • 12
  • 28

1 Answers1

1

You can use FasterCSV to parse a file a line-at-a-time.

From the FasterCSV documentation:

A Line at a Time
  FasterCSV.foreach("path/to/file.csv") do |row|
    # use row here...
  end

All at Once
  arr_of_arrs = FasterCSV.read("path/to/file.csv")

(Note: In your question, you are using FasterCSV.parse, which is only used to parse Strings into CSV. Because you want to use files, look at the .foreach and .parse methods, which will handle opening and scanning the file for you.).

cbeer
  • 1,221
  • 10
  • 13
  • Awesome, thanks. Wasn't sure if this would still load the file anyway. Good to know it doesn't. – Asherlc Aug 03 '12 at 18:46