TheDeveloperBlog.com


Ruby File Handling: File and IO Classes

Files. The nature of data involves persistence. In files, data persists longer than in memory. It lasts through restarts and can be transferred. It is durable.


Open. This example opens a file with File.open. It reads in each line of the file. It uses an until-loop to sequentially access each line, until the end of the file (eof).

Path: The file opened should be stored in the same directory as the program. The leading slash indicates this.

Tip: Reading in files line-by-line is often more efficient for long files than reading them all at once. It uses less memory.

Here: In this example, the data.readline() call returns the line read. It includes the trailing newline, if one exists.

Print: When we use the print method, the trailing newline is printed to the output.

Based on:

Ruby 2

Contents of data.txt

Line 1
Line 2
And line 3

Ruby program that uses File.open

# Open data file with open.
data = File.open("/data.txt")

# Until end-of-file.
until data.eof()
    # Read line.
    line = data.readline()
    # Print line.
    print line
end

Output

Line 1
Line 2
And line 3

Exists. A file that does not exist cannot be read. In clearer terms, we must first test a file for existence. In Ruby we use the "File.exists?" method for this.

True, false: The "exists?" predicate method returns true or false. We can test it directly in an if-statement.

Here: This program will likely print "No" on your system. If you place a "perls.txt" file in the root directory, it prints "Yes".

Ruby program that uses File.exists

# See if this file exists.
if File.exists?("/perls.txt")
    puts "Yes"
else
    puts "No"
end

Output

Yes

Size. How can you get the size, in bytes, of a file? A simple approach is to use File.stat and access its size property. This returns the number of bytes in the file.

Info: For this example, make sure to change the path to a file that exists. An exception is encountered if the file does not exist.

Ruby program that gets file size

# Get size of this file.
s = File.stat("/perls/p")
bytes = s.size

# Display the file size.
puts bytes

Output

106252

Readlines. It is easy to place an entire text file into an array of lines. This allows us to loop over and process the array, avoiding further disk reads.

Here: We invoke IO.readlines, which receives a file path argument. Often we can use relative paths.

Next: We read a file called "file.txt" in my "C:\perls\" folder. For your system, please change the path to one that exists.

Ruby program that uses readlines

lines = IO.readlines("/perls/file.txt")

# Display line count.
puts lines.length

# Loop over all lines in the array.
lines.each do |line|
    puts line
end

Output

3
Line one
Line two
Line three

IO.foreach. This method iterates over the lines in a file. We pass it the file location. We can use IO.foreach in a do-loop with an iteration variable or with an iterator block.

Iterators

Note: The first two lines have only eight characters, but have lengths of 9. The ending whitespace character "\n" is being counted.

Ruby program that uses IO.foreach

# Use IO.foreach with do.
IO.foreach("/perls/file.txt") do |line|
    puts line
end

# Display lengths of lines with block.
IO.foreach("/perls/file.txt") {|line| puts line.length}

Output

Line one
Line two
Line three
9
9
10

Comma-separated values. Split can be paired with an iterator like IO.foreach. In this way we can handle CSV files. An example is available for the split() method.

Split, File

Newlines. When we read lines from files, the trailing newlines are included. We must use a method like chomp() to clean up our strings.

Here: We call "chomp!" on each string returned from readline. Many other string methods can be used.

String, Chomp
File contents: gems.txt

ruby
sapphire
diamond
emerald
topaz

Ruby program that uses chomp, readline

data = File.open("/files/gems.txt")

until data.eof()
    line = data.readline()

    # Chomp line to remove trailing newline.
    line.chomp!
    puts "[" << line << "]"
end

Output

[ruby]
[sapphire]
[diamond]
[emerald]
[topaz]

Complete support. Ruby provides complete support for file handling. It accesses the file system with methods from the IO and File classes. IO is integrated into the language.