From: Tachikoma Date: 2008-07-17T11:30:08+09:00 Subject: Re: write multiple key/value to a row using cvs library On Jul 16, 10:56 pm, Li Chen wrote: > Hi all, > > I have a hash containing multiple key/values. I want to wirte all > key/value to the same row using cvs library. Is it possible to do that? > Now I follow the example from the library and I can only write each > key/value to one row and multiple rows from a hash. > > Thanks, > > Li > > ########## > EXAMPLE > >   Write rows to 'csvout' file. > >   outfile = File.open('csvout', 'wb') >   CSV::Writer.generate(outfile) do |csv| >     csv << ['c1', nil, '', '"', "\r\n", 'c2'] > >     ####here is my modified code### >     hash.each_pair{|k,v|csv<<"#{k}","#{v}"} > >     ... >   end > >   outfile.close > ############ > -- > Posted viahttp://www.ruby-forum.com/. I tried this: require 'csv' outfile = File.open('csvout','wb') hash = {"a" => 100,"b" => 200,"c" => 300} CSV::Writer.generate(outfile) do |csv| csv << hash.to_a.flatten end the result is : a,100,b,200,c,300 I hope this will help