1. Technology

Files and Directories

How to access files and directories in Ruby.

Reading Files
Programs rarely exist in their own little world. Even simple simulations need input data, and that data often comes from files.

Using Temporary Files
There are many hidden pitfalls when using temporary files. Ruby's Tempfile class solves many of these.

Dealing With Large Directories
It's tempting to gather up all of your directory entries in an Array and iterate over them in this way, but there's a good reason not to do this on large directories. Imagine you have a very large directory that holds thousands of images. For example, the frames of a movie about the be encoded. At 30 frames per second, the number of files in this directory is going to be very large. Gathering…

Using Glob with Directories
In the previous article in this series, we discussed the basics of the Dir class. Particularly useful was the Dir.foreach method for iterating over all files in a directory. However, this only goes so far. What if you want to iterate over only some files (for example, just XML files) in a directory?

Working with Directories
Learning how to work with files is one of the first things new Ruby programmers learn. However, how to work with directories of files is something that's often skipped or only lightly touched upon. Ruby does have some powerful mechanisms for working with directories though.

You can opt-out at any time. Please refer to our privacy policy for contact information.

©2014 About.com. All rights reserved.