Ruby – Using Ruby CSV to extract one column


I've been trying to work with getting a single column out of a csv file.

I've gone through the documentation,
but still don't really understand how to use it.

If I use CSV.table, the response is incredibly slow compared to I admit the dataset I'm loading is quite large, which is exactly the reason I only want to get a single column from it.

My request is simply currently looks like this

@dataTable = CSV.table('path_to_csv.csv')

and when I debug I get a response of

#<CSV::Table mode:col_or_row row_count:2104 >

The documentation says I should be able to use by_col(), but when I try to output

<%= debug @dataTable.by_col('col_name or index') %>

It gives me "undefined method 'col' error"

Can somebody explain to me how I'm supposed to use CSV? and if there is a way to get columns faster using 'read' instead of 'table'?

I'm using Ruby 1.92, which says that it is using fasterCSV, so I don't need to use the FasterCSV gem.

Best Solution

To pluck a column out of a csv I'd probably do something like the following:

col_data = []
CSV.foreach(FILENAME) {|row| col_data << row[COL_INDEX]}

That should be substantially faster than any operations on CSV.Table