Need pointers on writing a hash to a csv with keys forming the column names and the key values being the column values.
Hash is of the format as below
hash = { 'A' => [ 'v', 'x', 'y' , 'z' ] , 'B' => [ 'm', 'n' , 'o' ] ,
'C' => [ 'i', 'j' , 'k' , 'l', 'm', 'n' , 'o' ] }
Desired CSV output
row 0 (headers) - 'A' , 'B' , 'C'
row 1 - 'v' , 'm' , 'i'
row 2 - 'x' , 'n' , 'j'
row 3 - 'y' , 'o' , 'k'
row 4 - 'z' , '' , 'l'
row 5 - '' , '' , 'm'
row 6 - '' , '' , 'n'
row 7 - '' , '' , 'o'
Tried the following -
csv = CSV.open ("file.csv" , 'wb', headers: true)
hash.each do |k, v|
csv[k] = v # CSV::Table has a []= method
end
Was thinking csv[k] = v should work, but it doesn't.