Class

Dir

Extends:

Includes:

Objects of class Dir are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. See also File.

The directory used in these examples contains the two regular files (config.h and main.rb), the parent directory (..), and the directory itself (.).

Public Methods
[] Equivalent to calling Dir.glob(array,0) and Dir.glob(,0).
chdir Changes the current working directory of the process to the given string. When called without an argument, changes the directory to the value of the environment variable HOME, or LOGDIR. SystemCallError (probably Errno::ENOENT) if the target directory does not exist.
chroot Changes this process’s idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot(2) for more information.
close Closes the directory stream. Any further attempts to access dir will raise an IOError.
delete Deletes the named directory. Raises a subclass of SystemCallError if the directory isn’t empty.
each Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
entries Returns an array containing all of the filenames in the given directory. Will raise a SystemCallError if the named directory doesn’t exist.
foreach Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
getwd Returns the path to the current working directory of this process as a string.
glob Returns the filenames found by expanding pattern which is an Array of the patterns or the pattern String, either as an array or as parameters to the block. Note that this pattern is not a regexp (it’s closer to a shell glob). See File::fnmatch for the meaning of the flags parameter. Note that case sensitivity depends on your system (so File::FNM_CASEFOLD is ignored)
mkdir Makes a new directory named by string, with permissions specified by the optional parameter anInteger. The permissions may be modified by the value of File::umask, and are ignored on NT. Raises a SystemCallError if the directory cannot be created. See also the discussion of permissions in the class documentation for File.
new Returns a new directory object for the named directory.
open With no block, open is a synonym for Dir::new. If a block is present, it is passed aDir as a parameter. The directory is closed at the end of the block, and Dir::open returns the value of the block.
path Returns the path parameter passed to dir’s constructor.
pos Returns the current position in dir. See also Dir#seek.
pos= Synonym for Dir#seek, but returns the position parameter.
pwd Returns the path to the current working directory of this process as a string.
read Reads the next entry from dir and returns it as a string. Returns nil at the end of the stream.
rewind Repositions dir to the first entry.
rmdir Deletes the named directory. Raises a subclass of SystemCallError if the directory isn’t empty.
seek Seeks to a particular location in dir. integer must be a value returned by Dir#tell.
tell Returns the current position in dir. See also Dir#seek.
unlink Deletes the named directory. Raises a subclass of SystemCallError if the directory isn’t empty.
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.