the following code:
[1,3,5].to_csv
=> "1,3,5\n" # this is good
[[1,3,5], [2,4,6]].to_csv
=> "135,246\n" # why doesn't it just do it for array of array?
but require this instead:
data = [[1,3,5], [2,4,6]]
csv_string = FasterCSV.generate do |csv|
data.each {|a| csv << a}
end
=> "1,3,5\n2,4,6\n"
or shorter:
data = [[1,3,5], [2,4,6]]
csv_string = FasterCSV.generate {|csv| data.each {|a| csv << a}}
=> "1,3,5\n2,4,6\n"
The question is, when given an array of array, why is to_csv
not designed to handle it automatically, so that in Rails, we can do
respond_to do |format|
format.csv { render :text => data.to_csv }