How to Output Array to CSV in Ruby

Oluwafisayo Oluwatayo Feb 02, 2024
  1. What is CSV
  2. Create CSV File Using CSV.open Method in Ruby
  3. Create CSV File Using File.write Method in Ruby
  4. Generate the CSV File Inside the Terminal Using Ruby
  5. Conclusion
How to Output Array to CSV in Ruby

There are times when we are dealing with a long list of information, and we need to organize it so that it becomes easy to read; that is what the CSV brings to the table.

What is CSV

CSV means comma-separated values, which means that it formats data by separating them with commas.

Such an output format makes our data well organized in a tabular format so that when an individual goes through such data, he can easily read and edit them if needed.

CSV data can either be stored in its own .csv format or a more common .txt file.

Using the Ruby framework, let us look at various ways to create CSV files from the data in an array.

Create CSV File Using CSV.open Method in Ruby

In this example, we will create a CSV.open function and input the path where we want the file to be; then, we will input the array of data we want to organize in CSV format inside a CSV tag.

We will create a new file, name it new.rb and type in these codes:

require 'csv'
CSV.open('C:/Users/HP/Downloads/csv/outfield.txt', 'w') do |csv|
  csv << %w[one two three four]
  csv << %w[eight five]
  # ...
end

Once we run this code, we will see the outfield.txt file created in the path we stated in the CSV.open bracket. Then we will see that we added a "w" right beside the path to ensure that we can create a file inside the stated path.

using the csv open function

If we take away the "w" from there, we will see the error message: not opened for writing (IOError).

Create CSV File Using File.write Method in Ruby

This method doesn’t require that we add the "w" to the path in which we want the file to be created. This is because the File.write immediately opens the path for writing, then creates the file.

We will create a new file, name it new.rb and type in these codes:

tables = [%w[first second third], %w[army marine navy blackops], %w[James Bond 007]]
require 'csv'
File.write('C:/Users/HP/Downloads/csv/outfe.txt', tables.map(&:to_csv).join)

Here we have created the array inside the tables tag; then, we indicated the location we want the file to be created in inside the File.write() function.

Then we use the map function to return the data in the tables array in CSV format. When we run this code, after a few moments, we will see the new file created in the path we have stated.

using the file write function

Generate the CSV File Inside the Terminal Using Ruby

This method doesn’t create a file with the data in CSV format; instead, it displays the data right there in the terminal; this is great when we want to quickly use the results or to read the data in a more pleasing format.

We will create a new file and name it new.rb, then we will type in these codes:

require 'csv'
csv_string = CSV.generate do |csv|
  csv << %w[google bing ask search]
  csv << %w[find seek]
  # ...
end

The CSV.generate function takes the array in the csv brackets and generates the data in CSV format. Once we run the code, we will see the data in CSV format right inside the terminal.

string using terminal

Conclusion

The feature of the CSV to organize data clearly by separating them with commas makes it conducive to glancing through a long list of data.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

LinkedIn