File IO in Ruby

File IO in Ruby Featured Image

Welcome back to an ongoing series called File IO in Every Language! In this installment we’ll be taking a look at doing file IO in Ruby.

Table of Contents

File IO In Ruby

As usual, we present the whole solution before we delve deeper into the code:

def write_file
  out = File.new("output.txt", "w")

  out << "This is a line written by a Ruby program\n"
  out << "This line also"

  out.flush()
  out.close()
end


def read_file
  in_file = File.open("output.txt", "r")

  in_file.each_line do |line|
    puts line
  end

  in_file.close()
end

write_file()
read_file()

Before reading on, why not try to figure out how it works?

Writing

For modularity and readability, we have extracted the file writing code into its own function:

def write_file
  out = File.new("output.txt", "w")

  out << "This is a line written by a Ruby program\n"
  out << "This line also"

  out.flush()
  out.close()
end

On the first line we call the function File.new():

out = File.new("output.txt", "w")

It takes an argument to the path of the file and a mode. A mode is how we want the file to be opened. By default, it opens for reading. We want to write abilities so we specify “w” for writing.

For the next two lines we write arbitrary text to the file:

out << "This is a line written by a Ruby program\n"
out << "This line also"

In this sample code we used the bitshift left operators to write the text. We could have used the write() method to write text instead of using the operators.

Next, we flush the file:

out.flush()

Sometimes when we make calls to write not everything may get written down to disk. Only a fraction could be in the file and the rest is in memory waiting to get written to the file. To ensure everything is written, we call the method flush().

Lastly, we close the file to free up its resources:

out.close()

This is something you should always do with resources when you’re done with them.

Reading

Like with the write_file() function, we put the code for reading the file in its own function:

def read_file
  in_file = File.open("output.txt", "r")

  in_file.each_line do |line|
    puts line
  end

  in_file.close()
end

To begin, we open a file on the first line:

in_file = File.open("output.txt", "r")

File.open() takes an argument to the path of the file and a mode. By default, it opens the file for reading purposes, but for the sake of being explicit I passed “r” for the mode.

The next three lines loop through the file and print out each line:

in_file.each_line do |line|
  puts line
end

Finally, we close the file:

in_file.close()

And, that’s it for reading.

Scripting

Since Ruby is a scripting language, we can call the two functions we’ve just defined back-to-back at the end of our file:

write_file()
read_file()

And, that’ll do it!

How to Run the Solution

Websites like Repl allow you to write and run code of different programming languages in the browser. Feel free to drop our solution into one of your favorite online editors.

If you have Ruby installed on your computer/mac, you can run the following command:

ruby file-io.rb

To verify everything has worked correctly, you should see the contents of the file printed to the screen. In addition, the dummy file should be located alongside your script now. Feel free to check that out and let us know.

Sample Programs in Every Language

So far, we’ve covered three programming languages in this series. Naturally, we still have a long way ahead of us before we can say we’ve conquered all of the programming languages.

We appreciate it that you took your time to read this article, and we want to hear your feedback on this article and this series as a whole. Let us know how you’re feeling down below in the comments.

As always, if you want to receive regular updates from The Renegade Coder, we recommend you become a member. In addition to a regular newsletter, you’ll gain commenting privileges. That way, we can kick off a fun dialogue. See you next time!

Series Navigation← File IO in Python
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.