Asherlc
Asherlc

Reputation: 1161

Does FasterCSV open entire file on read?

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?

Upvotes: 0

Views: 841

Answers (1)

cbeer
cbeer

Reputation: 1231

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.).

Upvotes: 1

Related Questions