How can you parse just the first line of a CSV file?

ruby, ruby-on-rails

Solution

A better way of doing this is to simply use the built-in Enumerable support in Ruby's Standard Library CSV parser:

headers = CSV.open('file.csv', 'r') { |csv| csv.first }

The block will result in the file automatically being closed and the call will return an array of the parsed headers.

Problem

How can you parse just the first line of a CSV file? I want to make sure that all of the appropriate columns are provided in the file, but don't want to process the whole file.

Original source

Related problems