0
votes

We ran this code last year in using ruby 1.9.3 and Rails 3.0 with no problems, but we now need to run it again this year following an upgrade and it is failing.

The error appears to be around parsing headers when importing a CSV file. The code is

ROOT_DIR = File.expand_path File.dirname(__FILE__)
csv_file = ROOT_DIR + '/holidays_2016.csv'
csv = CSV.read(csv_file, headers: true)

The error returned is

output error: #<NoMethodError: undefined method `table' for #<Object:0x007f82898e15a8>>

removing the headers: true option fixes the problem but I really do need the headers as they are used later in the code !

1
Sidenote: CSV.read expects a hash as options parameter, not named parameters. It probably worked because of defaults, not because your code did not appear to have a bug. Take a look at source. CSV.read(csv_file, {headers: true}) should resolve your problem.Aleksei Matiushkin
Nope. Still the same issue. Running the code on last year's file which worked fine now reports the same error too.Gareth Burrows
Oh, I entangled in option names, sorry. It should be CSV.read(csv_file, {return_headers: true}) of course.Aleksei Matiushkin

1 Answers

0
votes

Try using return_headers: true

from the documentation:

:headers

If set to :first_row or true, the initial row of the CSV file will be treated as a row of headers. If set to an Array, the contents will be used as the headers. If set to a String, the String is run through a call of ::parse_line with the same :col_sep, :row_sep, and :quote_char as this instance to produce an Array of headers. This setting causes #shift to return rows as CSV::Row objects instead of Arrays and #read to return CSV::Table objects instead of an Array of Arrays.

:return_headers

When false, header rows are silently swallowed. If set to true, header rows are returned in a CSV::Row object with identical headers and fields (save that the fields do not go through the converters).