On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Dir
An object of class Dir represents a directory in the underlying file system.
It consists mainly of:
-
A string path, given when the object is created, that specifies a directory in the underlying file system; method
pathreturns the path. -
A collection of string entry names, each of which is the name of a directory or file in the underlying file system; the entry names may be retrieved in an array-like fashion or in a stream-like fashion.
About the Examples
Some examples on this page use this simple file tree:
example/ ├── config.h ├── lib/ │ ├── song/ │ │ └── karaoke.rb │ └── song.rb └── main.rb
Others use the file tree for the Ruby project itself.
Dir As Array-Like
A Dir object is in some ways array-like:
-
It has instance methods
children,each, andeach_child. -
It includes module Enumerable.
Dir As Stream-Like
A Dir object is in some ways stream-like.
The stream is initially open for reading, but may be closed manually (using method close), and will be closed on block exit if created by Dir.open called with a block. The closed stream may not be further manipulated, and may not be reopened.
The stream has a position, which is the index of an entry in the directory:
-
The initial position is zero (before the first entry).
-
Method
pos=sets the position (but ignores a value outside the stream), and returns the position. -
Method
seekis likepos=, but returnsself(convenient for chaining). -
Method
read, if not at end-of-stream, reads the next entry and increments the position; if at end-of-stream, does not increment the position. -
Method
rewindsets the position to zero.
Examples (using the simple file tree):
dir = Dir.new('example') # => #<Dir:example> dir.pos # => 0 dir.read # => "." dir.read # => ".." dir.read # => "config.h" dir.read # => "lib" dir.read # => "main.rb" dir.pos # => 5 dir.read # => nil dir.pos # => 5 dir.rewind # => #<Dir:example> dir.pos # => 0 dir.pos = 3 # => 3 dir.pos # => 3 dir.seek(4) # => #<Dir:example> dir.pos # => 4 dir.close # => nil dir.read # Raises IOError.
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 24
Instance methods 16
Ruby version changes 5
-
4.1
-
Dir.globsupports***/for recursive matching that follows directory symbolic links, stopping at links to ancestor directories. [Feature #17548]
-
-
3.3
-
Dir.for_fdadded for returning aDirobject for the directory specified by the provided directory file descriptor. [Feature #19347] -
Dir.fchdiradded for changing the directory to the directory specified by the provided directory file descriptor. [Feature #19347] -
Dir#chdiradded for changing the directory to the directory specified by the providedDirobject. [Feature #19347]
-
-
3.0
-
Dir.globandDir.[]now sort the results by default, and accept thesort:keyword option. [Feature #8709]
-