Ruby File Handling: File and IO ClassesHandle files: open a file with File.open and sequentially read in each line until eof.
dot net perls
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.
In Ruby, we have methods like readline and readlines() that can incrementally process files. This is usually best for performance.
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.
And line 3
# Open data file with open.
Ruby program that uses File.open
data = File.open
# Until end-of-file.
# Read line.
line = data.readline()
# Print line.
And line 3
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".
# See if this file exists.
Ruby program that uses File.exists
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.
# Get size of this file.
Ruby program that gets file size
s = File.stat("/perls/p"
bytes = s.size
# Display the file size.
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
# Display line count.
# Loop over all lines in the array.
lines.each do |line|
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.
Note The first two lines have only eight characters, but have lengths of 9. The ending whitespace character "\n" is being counted.
# Use IO.foreach with do.
Ruby program that uses IO.foreach
) do |line|
# Display lengths of lines with block.
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.
Ruby program that uses chomp, readline
data = File.open("/files/gems.txt"
line = data.readline()
# Chomp line to remove trailing newline.
puts "[" << line << "]"
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.
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.
© 2007-2021 sam allen. send bug reports to firstname.lastname@example.org.