File

Added in v1.8 · Updated March 13, 2026 · Modules
ruby stdlib file io

The File module provides utilities for working with files and directories in Ruby. It is part of the standard library and offers methods for reading, writing, and managing file paths.

Overview

The File class is Ruby’s interface to the operating system file system. It provides methods for:

  • Reading and writing file contents
  • Getting and setting file metadata
  • Working with file paths
  • Checking file existence and permissions

Opening and Reading Files

File.read reads the entire contents of a file into a string.

contents = File.read("config.txt")
puts contents

File.read Parameters

ParameterTypeDefaultDescription
filenameStringRequiredPath to the file
lengthIntegernilNumber of bytes to read
offsetIntegernilStarting position
modeString"r"File mode

Reading Line by Line

File.foreach iterates through a file line by line:

File.foreach("log.txt") do |line|
  puts line
end

Or using File.readlines to get an array of lines:

lines = File.readlines("data.csv")
lines.each { |line| puts line }

Writing Files

File.write writes content to a file:

File.write("output.txt", "Hello, World!")

File.write Parameters

ParameterTypeDefaultDescription
filenameStringRequiredPath to the file
dataStringRequiredContent to write
modeIntegernilFile mode (permissions)

Writing with Open Block

File.open with a block automatically closes the file:

File.open("output.txt", "w") do |file|
  file.puts "First line"
  file.puts "Second line"
end

File Modes

ModeDescription
”r”Read only (default)
“w”Write only, truncates file
”a”Append mode
”r+“Read and write
”w+“Read and write, truncates
”a+“Read and append
# Append to existing file
File.open("log.txt", "a") do |file|
  file.puts "New log entry"
end

Working with Paths

File.join combines path components:

path = File.join("home", "user", "documents", "file.txt")
# => "home/user/documents/file.txt" (Unix)

File.dirname gets the directory part:

File.dirname("/home/user/documents/file.txt")
# => "/home/user/documents"

File.basename gets the filename:

File.basename("/home/user/documents/file.txt")
# => "file.txt"

File.basename("/home/user/documents/file.txt", ".txt")
# => "file"

File.extname gets the extension:

File.extname("document.pdf")
# => ".pdf"

File Information

File.exist? checks if a file exists:

File.exist?("config.txt")  # => true or false

File.file? checks if it is a regular file (not directory):

File.file?("config.txt")   # => true or false

File.directory? checks if it is a directory:

File.directory?("folder")  # => true or false

Getting File Size

File.size("large_file.bin")  # => 1048576 (bytes)

Getting Modification Time

File.mtime("config.txt")
# => 2026-03-13 10:30:00 +0000

# Compare modification times
file1 = "file1.txt"
file2 = "file2.txt"
File.mtime(file1) > File.mtime(file2)  # => true/false

File Permissions

File.chmod changes file permissions:

File.chmod(0644, "script.rb")  # Read/write for owner, read for others

File.readable? checks if readable:

File.readable?("config.txt")  # => true or false

File.writable? checks if writable:

File.writable?("config.txt")  # => true or false

File.executable? checks if executable:

File.executable?("script.sh")  # => true or false

Deleting Files

File.delete or File.unlink removes a file:

File.delete("temp.txt")
File.unlink("temporary.dat")

Practical Examples

Reading CSV Files

require "csv"

File.foreach("data.csv") do |line|
  puts CSV.parse_line(line).inspect
end

Processing Log Files

error_count = 0
File.foreach("application.log") do |line|
  if line.include?("ERROR")
    error_count += 1
  end
end
puts "Found #{error_count} errors"

Safely Writing Configuration

# Write atomically using a temp file
require "tempfile"

temp = Tempfile.new("config")
temp.write(YAML.dump(config))
temp.close
FileUtils.mv(temp.path, "config.yml")

See Also