Skip to content

Instantly share code, notes, and snippets.

@wikiti
Last active November 12, 2021 09:38
Show Gist options
  • Save wikiti/bd496953079a5b5740bab5e97a59afd6 to your computer and use it in GitHub Desktop.
Save wikiti/bd496953079a5b5740bab5e97a59afd6 to your computer and use it in GitHub Desktop.
Generate CSV file or strings with ruby
require 'csv'
# To a file
CSV.open('path/to/file.csv', 'wb') do |csv|
csv << ['row', 'of', 'CSV', 'data']
csv << ['another', 'row']
# ...
end
# To a String
csv_string = CSV.generate do |csv|
csv << ['row', 'of', 'CSV', 'data']
csv << ['another', 'row']
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment