Abbey Workshop

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

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.

For more information, see the following books

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3