From: Ken Bloom Date: 2006-12-03T14:25:05+09:00 Subject: Re: comma separating output from array.to_s On Sat, 02 Dec 2006 20:22:20 -0800, William James wrote: > Gregory Brown wrote: >> On 12/2/06, William James wrote: >> > Gregory Brown wrote: >> > > On 12/2/06, jansenh wrote: >> > > > hi comp.lang.ruby >> > > > >> > > > what is the ruby-way of comma separating the output from array.to_s? >> > > >> > > some_array.join(",") >> > > >> > > but if you are thinking of doing CSV output, you should not use a >> > > naive approach like this, since it will likely produce malformed CSV >> > > when dealing with quoted text. >> > >> > > >> > puts [22,'He said, "No!"'].map{|x| x=x.to_s >> > x =~ /["\n]/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',') >> > 22,"He said, ""No!""" >> >> Yeah, now deal with edge cases, and try to run that on 100k rows :) > > It's easy to handle all cases since CSV is a simple format; > no pompous prolixity is needed: Most of use like pompous proxility as it helps us understand what the heck is going on. If I ran across your CSV regexp line in code I was debugging, I wouldn't know what it meant. > puts ['x',' y ','He said, "No!"'].map{|x| x=x.to_s > x =~ /["\n]|^\s|\s$/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',') > x," y ","He said, ""No!""" > > If that won't handle 100k rows, then fasterCsv probably won't either. -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/