On this page
End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Dir
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 (.).
What’s Here
First, what’s elsewhere. Class Dir:
-
Inherits from class Object.
-
Includes module Enumerable, which provides dozens of additional methods.
Here, class Dir provides methods that are useful for:
Reading
-
close: Closes the directory stream forself. -
pos=: Sets the position in the directory stream forself. -
read: Reads and returns the next entry in the directory stream forself. -
rewind: Sets the position in the directory stream forselfto the first entry. -
seek: Sets the position in the directory stream forselfthe entry at the given offset.
Setting
-
::chdir: Changes the working directory of the current process to the given directory. -
::chroot: Changes the file-system root for the current process to the given directory.
Querying
-
::children: Returns an array of names of the children (both files and directories) of the given directory, but not including.or... -
::empty?: Returns whether the given path is an empty directory. -
::entries: Returns an array of names of the children (both files and directories) of the given directory, including.and... -
::exist?: Returns whether the given path is a directory. -
::getwd(aliased as pwd): Returns the path to the current working directory. -
::glob: Returns an array of file paths matching the given pattern and flags. -
::home: Returns the home directory path for a given user or the current user. -
children: Returns an array of names of the children (both files and directories) ofself, but not including.or... -
fileno: Returns the integer file descriptor forself. -
path(aliased asto_path): Returns the path used to createself. -
tell(aliased aspos): Returns the integer position in the directory stream forself.
Iterating
-
::each_child: Calls the given block with each entry in the given directory, but not including.or... -
::foreach: Calls the given block with each entry in the given directory, including.and... -
each: Calls the given block with each entry inself, including.and... -
each_child: Calls the given block with each entry inself, but not including.or...
Other
-
::mkdir: Creates a directory at the given path, with optional permissions. -
::new: Returns a new Dir for the given path, with optional encoding. -
::open: Same as::new, but if a block is given, yields the Dir to the block, closing it upon block exit. -
::unlink(aliased as::deleteand::rmdir): Removes the given directory. -
inspect: Returns a string description ofself.
Class methods 21
Instance methods 14
Ruby version changes 1
-
3.0
-
Dir.globandDir.[]now sort the results by default, and accept thesort:keyword option. [Feature #8709]
-