Skip to content

Instantly share code, notes, and snippets.

@k2works
Created December 24, 2014 02:50
Show Gist options
  • Save k2works/36812bc8db8eb5142c92 to your computer and use it in GitHub Desktop.
Save k2works/36812bc8db8eb5142c92 to your computer and use it in GitHub Desktop.
ヘッダ付きCSVデータを生成する
require 'csv'
headers = %w(name age gender)
people = [
['alice',14,'female'],
['bob',32,'unknown'],
['carol',17,'female']
]
csv_string = CSV.generate('', write_headers: true, headers: headers) { |csv|
people.each do |person|
csv << person
end
}
puts csv_string
# >> name,age,gender
# >> alice,14,female
# >> bob,32,unknown
# >> carol,17,female
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment