Ruby: The Basics of Reading a File
This howto covers the basics of reading a text file in Ruby. As with most scripting languages there is more than one way to do this, but hopefully the most straight forward methods are covered here. Below are three different examples of reading a text file. Each example reads a file and prints the file along with the line numbers for that file.
readfile.rb
1:#!/usr/bin/ruby -w 2: 3:# Created by Michael Williams 12/19/2005 4:# Licensed under Create Commons Attribution License 5: 6:# Example 1 - Read File and close 7:counter = 1 8:file = File.new("readfile.rb", "r") 9:while (line = file.gets) 10: puts "#{counter}: #{line}" 11: counter = counter + 1 12:end 13:file.close 14: 15:# Example 2 - Pass file to block 16:File.open("readfile.rb", "r") do |infile| 17: while (line = infile.gets) 18: puts "#{counter}: #{line}" 19: counter = counter + 1 20: end 21:end 22: 23:# Example 3 - Read File with Exception Handling 24:counter = 1 25:begin 26: file = File.new("readfile.rb", "r") 27: while (line = file.gets) 28: puts "#{counter}: #{line}" 29: counter = counter + 1 30: end 31: file.close 32:rescue => err 33: puts "Exception: #{err}" 34: err 35:end 36:
Download source for: readfile.rb
Different Ways to Read a File
The first example on lines 4-11 shows a method for reading the file that would seem pretty typical. On line 6, a file object is created with the new
method and the file is open for reading. The file name and read/write mode is specified. Your file mode options are as follows:
Mode | Description |
---|---|
r | Read-only, starts at beginning of file (default mode). |
r+ | Read-write, starts at beginning of file. |
w | Write-only, truncates existing file to zero length or creates a new file for writing. |
w+ | Read-write, truncates existing file to zero length or creates a new file for reading and writing. |
a | Write-only, starts at end of file if file exists, otherwise creates a new file for writing. |
a+ | Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing. |
The while
loop is defined on line 7. Notice that the gets
method is used to read each line. Ruby has a readline
method as well, but it raises an exception when the end of the file is reached. The gets
method returns nil
when the end of the file is reached. Thus it is preferable as no error is generated.
The second example reads the file by passing it to a block. Notice that on line 14 the open
method is used instead of new. This passes the file right to the block without having to create an object. The advantage of this approach is that you don't have to close the file with the close
method.
The third example shows you how to gracefully catch an exception and exit the application if something goes wrong. This is similar to a die
statement in Perl for a try/catch
block in Java. The code is surrounded by a begin
and end
block. A rescue
statement follows the code and will catch any exception thrown and print the error message. Of course there is more to Ruby exceptions, but that is not covered in this how to.
Content last updated 10/2012