In Chapter 5, Delimited Data, we looked at reading from CSV files, a common need when it comes to acquiring text for your program. But it’s often necessary to write CSV files, too.
We can do this using the same CSV library that we used to read the files. It has a capable interface for generating CSV data, writing directly to files, and modifying existing CSVs.
There are generally two reasons we’d want to write a CSV file. The first is when we want to take a data structure from our Ruby code and write it out to a CSV. The second is when we want to take an existing CSV file and modify it. These two different tasks have two different approaches. Let’s take a look at them in turn.
If we’re generating the data that ...