Skip to content

class Dir
inherits Reference

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.

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

See also: File.

Included modules

Enumerable Iterable

Class methods

.[](*patterns : Path | String, match_hidden = false, follow_symlinks = false) : Array(String)

Returns an array of all files that match against any of patterns.

The pattern syntax is similar to shell filename globbing, see File.match? for details.

Note

Path separator in patterns needs to be always /. The returned file names use system-specific path separators.

View source

.[](patterns : Enumerable, match_hidden = false, follow_symlinks = false) : Array(String)

Returns an array of all files that match against any of patterns.

The pattern syntax is similar to shell filename globbing, see File.match? for details.

Note

Path separator in patterns needs to be always /. The returned file names use system-specific path separators.

View source

.cd(path : Path | String) : String

Changes the current working directory of the process to the given string.

View source

.cd(path : Path | String

Changes the current working directory of the process to the given string and invokes the block, restoring the original working directory when the block exits.

View source

.children(dirname : Path | String) : Array(String)

See #children.

View source

.current : String

Returns the current working directory.

View source

.delete(path : Path | String)

Removes the directory at the given path.

View source

.each(dirname : Path | String, & : String -> )

See #each.

View source

.each_child(dirname : Path | String, & : String -> )

View source

.empty?(path : Path | String) : Bool

Returns true if the directory at path is empty, otherwise returns false. Raises File::NotFoundError if the directory at path does not exist.

Dir.mkdir("bar")
Dir.empty?("bar") # => true
File.write("bar/a_file", "The content")
Dir.empty?("bar") # => false
View source

.entries(dirname : Path | String) : Array(String)

See #entries.

View source

.exists?(path : Path | String) : Bool

Returns true if the given path exists and is a directory

View source

.glob(patterns : Enumerable, match_hidden = false, follow_symlinks = false, &block : String -> _)

Yields all files that match against any of patterns.

The pattern syntax is similar to shell filename globbing, see File.match? for details.

If match_hidden is true the pattern will match hidden files and folders.

Note

Path separator in patterns needs to be always /. The returned file names use system-specific path separators.

View source

.glob(*patterns : Path | String, match_hidden = false, follow_symlinks = false) : Array(String)

Returns an array of all files that match against any of patterns.

The pattern syntax is similar to shell filename globbing, see File.match? for details.

If match_hidden is true the pattern will match hidden files and folders.

Note

Path separator in patterns needs to be always /. The returned file names use system-specific path separators.

View source

.glob(patterns : Enumerable, match_hidden = false, follow_symlinks = false) : Array(String)

Returns an array of all files that match against any of patterns.

The pattern syntax is similar to shell filename globbing, see File.match? for details.

If match_hidden is true the pattern will match hidden files and folders.

Note

Path separator in patterns needs to be always /. The returned file names use system-specific path separators.

View source

.glob(*patterns : Path | String, match_hidden = false, follow_symlinks = false, &block : String -> _)

Yields all files that match against any of patterns.

The pattern syntax is similar to shell filename globbing, see File.match? for details.

If match_hidden is true the pattern will match hidden files and folders.

Note

Path separator in patterns needs to be always /. The returned file names use system-specific path separators.

View source

.mkdir(path : Path | String, mode = 511) : Nil

Creates a new directory at the given path. The linux-style permission mode can be specified, with a default of 777 (0o777).

Note

mode is ignored on windows.

View source

.mkdir_p(path : Path | String, mode = 511) : Nil

Creates a new directory at the given path, including any non-existing intermediate directories. The linux-style permission mode can be specified, with a default of 777 (0o777).

View source

.open(path : Path | String, & : self -> )

Opens a directory and yields it, closing it at the end of the block. Returns the value of the block.

View source

.tempdir : String

Returns the tmp dir used for tempfile.

Dir.tempdir # => "/tmp"
View source

.new(path : Path | String)

Returns a new directory object for the named directory.

View source

.open(path : Path | String) : self

Alias for new(path)

View source

Methods

#children : Array(String)

Returns an array containing all of the filenames except for . and .. in the given directory.

View source

#close : Nil

Closes the directory stream.

View source

#each(& : String -> ) : Nil

Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.

Dir.mkdir("testdir")
File.write("testdir/config.h", "")

d = Dir.new("testdir")
d.each { |x| puts "Got #{x}" }

produces:

Got .
Got ..
Got config.h
View source

#each : Iterator(String)

Must return an Iterator over the elements in this collection.

View source

#each_child(& : String -> ) : Nil

Calls the block once for each entry except for . and .. in this directory, passing the filename of each entry as a parameter to the block.

Dir.mkdir("testdir")
File.write("testdir/config.h", "")

d = Dir.new("testdir")
d.each_child { |x| puts "Got #{x}" }

produces:

Got config.h
View source

#each_child : Iterator(String)

View source

#entries : Array(String)

Returns an array containing all of the filenames in the given directory.

View source

#inspect(io : IO) : Nil

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
View source

#pretty_print(pp)

View source

#read : String?

Reads the next entry from dir and returns it as a string. Returns nil at the end of the stream.

d = Dir.new("testdir")
array = [] of String
while file = d.read
  array << file
end
array.sort # => [".", "..", "config.h"]
View source

#rewind : self

Repositions this directory to the first entry.

View source

#to_s(io : IO) : Nil

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>
View source