Make a list into a line suitable for a CSV file in Ruby
1 2 def comma_separate(items) 3 items.map! do |item| 4 if item.is_a?(String) and item =~ /[",]/ 5 '"' + item.gsub(/"/, '""') + '"' 6 else 7 item 8 end 9 end 10 items.join(',') 11 end
DZone Snippets > timmorgan > csv
13503 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Tim Morgan http://timmorgan.org
1 2 def comma_separate(items) 3 items.map! do |item| 4 if item.is_a?(String) and item =~ /[",]/ 5 '"' + item.gsub(/"/, '""') + '"' 6 else 7 item 8 end 9 end 10 items.join(',') 11 end