Posted 3 months ago. Visible to the public.

Ruby: Reading and writing CSVs

In ruby you can easily read and write CSVs with the standard CSV library class.

On top of this, you can use the gem smarter_csv for reading (not writing) CSVs in a more comfortable way:

  • Keep in mind, that the development of this gem is in an unknown state and the 2.0 release seems to happen never
  • The API will change completely for 2.0, so you might find a bunch of unrelated documentation for 1.2

Here is an example, what smarter_csv does for you:


first name,last name,dogs,cats,birds,fish Dan,McAllister,2,,, Lucy,Laweless,,5,, Miles,O'Brian,,,,21 Nancy,Homes,2,,1,


[ {first_name: 'Dan', last_name: 'McAllister', dogs: '2'}, {first_name: 'Lucy', last_name: 'Laweless', cats: '5'}, {first_name: 'Miles', last_name: "O'Brian", fish: '21'}, {first_name: 'Nancy', last_name: 'Homes', dogs: '2', birds: '1'} ]

You might want to keep empty lines, use string keys or change the encoding. This is all possible, look up for the proper options.


We used this helper in some project to do not have to write comma separated values, but use data objects instead.

Place this file in spec/supper/csv_helper.rb.

module CsvHelpers def generate_csv(csv_data) csv_string = CSV.generate do |csv| csv << csv_data.first.keys csv_data.inject(csv) { |acc, row| acc << row.values } end file = file.write(csv_string) file.rewind file end def generate_empty_csv file = file.write(' ') file.rewind file end end RSpec.configure do |c| c.include CsvHelpers end

The you can generate a csv file like this:

let(:csv_data) do [ { 'first_name' => 'Dan', 'last_name' => 'McAllister', 'dogs' => '2', 'cats' => nil, 'fish' => nil, 'birds' => nil }, { 'first_name' => 'Lucy', 'last_name' => 'Laweless', 'dogs' => nil, 'cats' => '5', 'fish' => nil, 'birds' => nil }, { 'first_name' => 'Miles', 'last_name' => "O'Brian", 'dogs' => nil, 'cats' => nil, 'fish' => '21', 'birds' => nil }, { 'first_name' => 'Nancy', 'last_name' => 'Homes', 'dogs' => '2', 'cats' => nil, 'fish' => nil, 'birds' => '1' } ] end let(:csv) { generate_csv(csv_data) } # This will return a CSV with one header row and four data rows

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Emanuel De
Last edit:
about 1 month ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more