What's the best way to parse a tab-delimited file in Ruby?

mbm picture mbm · Dec 10, 2010 · Viewed 27.2k times · Source

What's the best (most efficient) way to parse a tab-delimited file in Ruby?

Answer

jergason picture jergason · Dec 10, 2010

The Ruby CSV library lets you specify the field delimiter. Ruby 1.9 uses FasterCSV. Something like this would work:

require "csv"
parsed_file = CSV.read("path-to-file.csv", { :col_sep => "\t" })