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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-23 19:10
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 method `header_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 <main>'
# ~>   from -:15:in `each'
# ~>   from -:15:in `<main>'
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-23 20:14
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 ?
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2014-02-25 01:05
(Received via mailing list)
Dear Arup,

Look at
http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/C...

"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.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-25 09:09
Abinoam Jr. wrote in post #1137892:
> Dear Arup,
>
> Look at
> http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/C...
>
> "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/C...
) not `#[]`.

In the default mixed mode, this method **assigns rows for index access
and columns for header access.**
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-25 09:09
Abinoam Jr. wrote in post #1137892:
> Dear Arup,
>
> Look at
> http://ruby-doc.org/stdlib-2.1.0/libdoc/csv/rdoc/C...
>
> "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/C...
) not `#[]`.

In the default mixed mode, this method **assigns rows for index access
and columns for header access.**
This topic is locked and can not be replied to.