Build a CSV File Parsing System in Ruby

3
1247

This coding exercise will walk through how to work with CSV files and convert a file’s content into Ruby collections that you can utilize.

Summary

Build a CSV File parser in Ruby that converts a CSV file to array values.

Exercise File

Code File

Exercise Description

This coding exercise examines how to build a method that parses a CSV file, stores each line as an array, with each column as a separate element in the array.

Sample File

large

Example Data Output

[
  ["Name", "Title", "Email"],
  ["Darth Vader", "CEO", "[email protected]"],
  ["Luke Skywalker", "Dev", "[email protected]"],
  ["Kylo Ren", "COO", "[email protected]"],
]

Real World Usage

There are many times, when building a Ruby application, that you need to convert a large amount of data from a CSV file and utilize it in an application. This exercise will test your ability to:

  • Work with file paths
  • Convert string based data to arrays
  • Implement regular expressions for parsing data

Solution

Can be found on the solutions branch on github.

3 COMMENTS

  1. I’m having issues with this one. Inspecting the file like so

    p File.read(‘support/crm.csv’)

    I get

    “Name,Title,Email\nDarth Vader,CEO,[email protected]\nLuke Skywalker,Dev,[email protected]\nKylo Ren,COO,[email protected]\n”

    and then what the test gives me

    got: [[“Name”, “Title”, “Email\nDarth Vader”, “CEO”, “[email protected]\nLuke Skywalker”, “Dev”, “[email protected]ssues.com\nKylo Ren”, “COO”, “[email protected]\n”]]

LEAVE A REPLY

Please enter your comment!
Please enter your name here