How To Read & Write Files in Ruby (With Examples)

Today you’ll learn how to read & write files in Ruby so you can extract the contents, create new files, and find the information you need!

ruby read file

Let’s do this!

How to Read Files In Ruby

You can read a file in Ruby like this:

  1. Open the file, with the open method.
  2. Read the file, the whole file, line by line, or a specific amount of bytes.
  3. Close the file, with the close method.

Here is the process in detail.

Use the File class to open a file:

file ="users.txt")

As a result you’ll get a File object, but not the contents of the file yet.


You can read the contents of the file in three ways.

First, you can read the whole file.

Like this:

file_data =

# "user1\nuser2\nuser3\n"

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


file_data =

# ["user1", "user2", "user3"]

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

Like this:


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

file_data ="user.txt").split

# ["user1", "user2", "user3"]

One more tip on reading files.

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


File.foreach("users.txt") { |line| puts line }

Instead of reading the whole file into memory you’ll be able to process the file one line at a time, which is useful for big files.

How to Write to a File in Ruby

If you want to write to a file using Ruby:

  1. Open the file in write mode (“w” flag)
  2. Use the write method to add data to the file
  3. If you didn’t use the block version, remember to close

Example:"log.txt", "w") { |f| f.write "#{} - User logged in\n" }


This will rewrite the previous file contents!

If you want to add new content to the file, use the “a” (append) flag, instead of the “w” (write) flag.

One shortcut is to use File.write:

File.write("log.txt", "data...")

To use this method in append mode:

File.write("log.txt", "data...", mode: "a")

That’s the easiest way to write to a file in Ruby in just one line of code 🙂

One more thing…

If you want to write an array to a file you’ll have to convert it into a string first.

Here’s how:

File.write("log.txt", [1,2,3].join("\n"), mode: "a")

This process of converting an object into a string is called serialization.

Ruby File Methods

You can do other things with files, besides reading & writing to them.

For example, you may want to know if a file exists or to get a list of files for the current directory.

You are going to be using methods like:

  • rename
  • size
  • exists?
  • extname
  • basename
  • dirname
  • directory?
  • file?

Let’s see a few examples:

# Renaming a file
File.rename("old-name.txt", "new-name.txt")

# File size in bytes

# Does this file already exist?

# Get the file extension, this works even if the file doesn't exists
# => ".txt"

# Get the file name without the directory part
# => "ebook.pdf"

# Get the path for this file, without the file name
# => "/tmp"

# Is this actually a file or a directory?"cats")

The last example makes more sense if you are looping through the contents of a directory listing.

def find_files_in_current_directory
  entries = Dir.entries(".")

  entries.reject { |entry| }

You can also get stats for a file, like file size, permissions, creation date, etc:


Directory Operations

Using Dir.glob you can get a list of all the files that match a certain pattern.

Here are some examples:

# All files in current directory

# All files containing "spec" in the name

# All ruby files

This one line of code will recursively list all files in Ruby, starting from the current directory:


Use this if you only want to search for directories:


Using the Dir class it’s also possible to print the current working directory:


Check if a directory is empty:

# false

Check if a directory exists:

# true

Create a new directory:


Create a temporary directory with mktmpdir:

Dir.mktmpdir do |dir|
  File.write(dir + "/log.txt", "test")

How to Use The FileUtils Module

There are some extra file handling utilities you can get access to within the FileUtils module.

For example, you can compare files, touch a file (to update the last access & modification time), or copy files & directories with cp_r.

Like this:

require 'fileutils'

FileUtils.compare_file("a.txt", "b.txt")
FileUtils.cp_r("data", "backup")

Btw the “r” in cp_r stands for “recursive”.


You have learned how to manage files & folders in Ruby using built-in methods like & File.write.

Thanks for reading!