Undefined method `header_row?' for ["A", nil]:Array (NoMethodError)

Can anyone tell me why I am getting the error ?

require ‘csv’

_output_file_path =
File.expand_path(‘output.csv’,File.dirname(FILE))

hash = { ‘A’ => [ ‘v’, ‘x’, ‘y’ , ‘z’ ] ,
‘B’ => [ ‘m’, ‘n’ , ‘o’ ] ,
‘C’ => [ ‘i’, ‘j’ , ‘k’ , ‘l’, ‘m’, ‘n’ , ‘o’ ]
}

headers = hash.keys
csv_row = CSV::Row.new(headers,[],true)
csv_table = CSV::Table.new(csv_row)

headers.each do |col_name|
csv_table[col_name] = hash[col_name]
end

p csv_table.to_csv

~> /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/csv.rb:726:in

block in []=': undefined methodheader_row?’ for [“A”, nil]:Array
(NoMethodError)

~> from

/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/csv.rb:504:in
`each’

~> from

/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/csv.rb:504:in
`each’

~> from

/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/csv.rb:725:in
`each_with_index’

~> from

/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/csv.rb:725:in `[]=’

~> from -:16:in `block in ’

~> from -:15:in `each’

~> from -:15:in `’

I have fixed the error. But csv_table not showing any data .

require ‘csv’

_output_file_path =
File.expand_path(‘output.csv’,File.dirname(FILE))

hash = { ‘A’ => [ ‘v’, ‘x’, ‘y’ , ‘z’ ] ,
‘B’ => [ ‘m’, ‘n’ , ‘o’ ] ,
‘C’ => [ ‘i’, ‘j’ , ‘k’ , ‘l’, ‘m’, ‘n’ , ‘o’ ]
}

headers = hash.keys
csv_row = CSV::Row.new(headers,[],true)
csv_table = CSV::Table.new([csv_row])

headers.each do |col_name|
csv_table[col_name] = hash[col_name]
end
csv_table.to_csv

=> “A,B,C\n”

What wrong I did here ?

Dear Arup,

Look at
http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/CSV/Table.html#method-i-5B-5D

“Columns are returned as an Array of values. Altering that Array has
no effect on the table.”

headers.each do |col_name|
p csv_table[col_name].class # Add this line
csv_table[col_name] = hash[col_name]
end

And you will see:
Array
Array
Array

Abinoam Jr.

Abinoam Jr. wrote in post #1137892:

Dear Arup,

Look at
http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/CSV/Table.html#method-i-5B-5D

“Columns are returned as an Array of values. Altering that Array has
no effect on the table.”

But I am using the method #[]=
(http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/CSV/Table.html#method-i-5B-5D-3D
) not #[].

In the default mixed mode, this method assigns rows for index access
and columns for header access.

Abinoam Jr. wrote in post #1137892:

Dear Arup,

Look at
http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/CSV/Table.html#method-i-5B-5D

“Columns are returned as an Array of values. Altering that Array has
no effect on the table.”

But I am using the method #[]=
(http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/CSV/Table.html#method-i-5B-5D-3D
) not #[].

In the default mixed mode, this method assigns rows for index access
and columns for header access.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs