How To Read & Write Files in Ruby

Javier Goncalves
4 min readSep 10, 2020

--

Sometimes you’ll have to open csv, txt or other kind of files with ruby and work with the data that you are getting from that file and then probably write a new file with your solution.

What you will learn:

  1. How to read files in Ruby
  2. How to write files in Ruby
  3. Real examples to apply in the future

How to read files in Ruby

First you have to use the class File to open a file:

Now that you opened the file, you can read it in three different ways.

If you’re working with a file that has multiple lines you can either split("\n") the file_contentremoving the new lines, or use the readlines method plus the chomp method to remove the new line characters.

Either way will give u an array with multiple elements.

When you’re done working with a file you want to close it to free up memory & system resources.

As an alternative to having to open & close the file, you can use the File.read method:

This will return an array with all the data from that file.

If you want to process a file one line at a time, you can use the foreach method.

This is useful for big files, instead of reading the whole file into the memory, you will process the file one line at a time.

How to write to a files in Ruby

You can write files in a couple of different ways but its always using the write method.

It takes at least two parameters, the first one the name of the new file that you are writing, and the second the content that its being sent to the file.

This is how:

In a more realistic situation, you will probably have all your content in a hash or array. If you want to write the file from an array, keep in mind that you have to convert it into a string first.

Example:

export_file is the array with all the content for the new file that you are creating.

“data-out.txt” is the name of the file

export_file.join(“\n”) is adding a new line for each element.

Real examples

Lets say a client is asking you to make a program that gives the total result of a file with groceries and their prices. For example Apple, 2, lets assume that 2 is the price of the apple.

The client gave us a csv file with all the data. The client just want the total number of articles and price.

Lets assume the name of the file is “all-groceries.csv”

This is how your code editor will read the file.

Now lets open the file in our ruby file:

Output of file-content:

Each two elements represent the article and the price, we can say file_content[0] is the name of the article and file_content[1] is the price.

Knowing the order of the array, we can now loop and get exactly what we want.

Total would be equal to 31 but we still need the quantity of articles from this groceries file.

The quantity will be calculate getting the length of the array and dividing it by 2, don’t forget the order of our array, we know that every article and price takes 2 elements in our array, so if the length is 16 the real quantity will be 8. (Remember this is a very basic app that calculate a very simple file.)

Now its time to write our file:

This is how our folder looks like:

This is our generated file:

--

--