In Ruby, we have methods like readline and readlines() that can incrementally process files. This is usually best for performance.
Ruby is not a low-level language, and file handling reflects this. Usually we do not even need a loop to read in a file in this language.
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).
data.readline() call returns the line read. It includes the trailing newline, if one exists.# 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
endLine 1
Line 2
And line 3ExistsA 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.
if-statement.# See if this file exists.
if File.exists?("/perls.txt")
puts "Yes"
else
puts "No"
endYesSizeHow 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.
# Get size of this file.
s = File.stat("/perls/p")
bytes = s.size
# Display the file size.
puts bytes106252It 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.
IO.readlines, which receives a file path argument. Often we can use relative paths.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
end3
Line one
Line two
Line threeIO.foreachThis 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.
# 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}Line one
Line two
Line three
9
9
10When we read lines from files, the trailing newlines are included. We must use a method like chomp() to clean up our strings.
string returned from readline. Many other string methods can be used.data = File.open("/files/gems.txt")
until data.eof()
line = data.readline()
# Chomp line to remove trailing newline.
line.chomp!
puts "[" << line << "]"
end[ruby]
[sapphire]
[diamond]
[emerald]
[topaz]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.
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.