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

Working with Directories

By

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. Though Ruby does have some powerful mechanisms for working with directories.

The Dir class houses a number of methods for working with directories. Among these methods are small utility method such as Dir.pwd to get the current working directory and Dir.chdir to change the current working directory. The following is an explanation of some of the simpler methods Dir provides. These are all static methods of the Dir class.

  • Dir.chdir(directory) - Change the current working directory to directory. The parameter should be a string, either an absolute or relative path. The directory name '..' can be used to change directory to the current working directory's parent directory.

  • Dir.pwd - Return the current working directory as a string.

  • Dir.delete(directory) or Dir.rmdir(directory) or Dir.unlink(directory) - Delete a directory from the filesystem. Note that this method will fail if there are any files or directories in the directory to be deleted. This method will only work on empty directories.

  • Dir.mkdir(directory) - Create a new directory in the current working directory, or at the relative or absolute path given. This method will fail if such a directory already exists, or you do no have permission to create a directory at that location.

  • Dir.entries(directory) - Return an array of files in a directory as strings. All files and directories, including the directories . and .. will be returned. This directory listing is only one level deep, meaning only the files in the directory requested will be returned, not the files and directories further down the directory tree.

  • Dir.foreach(directory) {|f| ... } - This is a convenience function. It will iterate over all of the files in the directory requested and pass the name of each as a string to the block. This is equivalent to using Dir.entries(directory).each {|f| ... } however, depending on your platform's implementation, it may not gather all entries in an array first. For this reason, it may be more efficient than iterating over an array returned by Dir.entries.

With just these six methods, a majority of tasks dealing with directories can be accomplished. However, there is much more to Ruby. As these methods are simply thin wrappers around common operating system functions, there are often more Ruby-like solutions. See the other articles in this series for more information.

The following is a short example program demonstrating the use of some of these methods.

 #!/usr/bin/env ruby
 
 # Change to the directory passed in ARGV
 Dir.chdir(ARGV[0])
 
 # Print the current working directory
 puts Dir.pwd
 
 # Iterate over all files and directories
 # in the current working directory and
 # print them
 Dir.entries('.').each do|f|
   puts " #{f}"
 end
 
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Files and Directories
  6. Working with Directories

©2014 About.com. All rights reserved.