Easy Tutorial
❮ Ruby Operator Ruby Encoding ❯

Ruby Dir Class and Methods

Dir is a class that represents a directory stream used to list filenames in an operating system directory. The Dir class also provides operations related to directories, such as wildcard filename matching and changing the working directory.

Class Methods

No. Method & Description
1 Dir[pat]<br>Dir::glob(pat) <br>Returns an array containing filenames that match the specified wildcard pattern pat: * - matches any string including an empty string<br>* - matches any string recursively<br>? - matches any single character<br>[...] - matches any one of the enclosed characters<br>{a,b...} - matches any one of the strings<br>Dir["foo."] # matches "foo.c", "foo.rb", etc.<br>Dir["foo.?"] # matches "foo.c", "foo.h", etc.
2 Dir::chdir(path) <br>Changes the current directory.
3 Dir::chroot(path) <br>Changes the root directory (only allowed for superusers). Not available on all platforms.
4 Dir::delete(path) <br>Deletes the directory specified by path. The directory must be empty.
5 Dir::entries(path) <br>Returns an array containing filenames in the directory specified by path.
6 Dir::foreach(path) { f ...} <br>Executes the block once for each file in the directory specified by path.
7 Dir::getwd<br>Dir::pwd <br>Returns the current directory.
8 Dir::mkdir(path[, mode=0777]) <br>Creates the directory specified by path. The permission mode can be modified by the value of File::umask and is ignored on Win32 platforms.
9 Dir::new(path)<br>Dir::open(path)<br>Dir::open(path) { dir ...} <br>Returns a new directory object for the specified path. If open is given a block, the new directory object is passed to the block, and the directory object is closed before the block terminates.
10 Dir::pwd <br>See Dir::getwd.
11 Dir::rmdir(path)<br>Dir::unlink(path)<br>Dir::delete(path) <br>Deletes the directory specified by path. The directory must be empty.

Instance Methods

Assume d is an instance of the Dir class:

No. Method & Description
1 d.close <br>Closes the directory stream.
2 d.each { f ...} <br>Executes the block once for each entry in d.
3 d.pos <br>d.tell <br>Returns the current position in d.
4 d.pos= offset <br>Sets the position in the directory stream.
5 d.pos= pos<br>d.seek(pos) <br>Moves to a position in d. pos must be a value returned by d.pos or 0.
6 d.read <br>Returns the next entry in d.
7 d.rewind <br>Moves the position in d to the first entry.
8 d.seek(pos) <br>See d.pos=pos.
9 d.tell <br>See d.pos.
❮ Ruby Operator Ruby Encoding ❯