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)
¶
(*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.
.[](patterns : Enumerable, match_hidden = false, follow_symlinks = false) : Array(String)
¶
(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.
.cd(path : Path | String) : String
¶
(path : Path | String) : String
Changes the current working directory of the process to the given string.
.cd(path : Path | String
¶
(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.
.empty?(path : Path | String) : Bool
¶
(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
.exists?(path : Path | String) : Bool
¶
(path : Path | String) : Bool
Returns true
if the given path exists and is a directory
.glob(patterns : Enumerable, match_hidden = false, follow_symlinks = false, &block : String -> _)
¶
(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.
.glob(*patterns : Path | String, match_hidden = false, follow_symlinks = false) : Array(String)
¶
(*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.
.glob(patterns : Enumerable, match_hidden = false, follow_symlinks = false) : Array(String)
¶
(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.
.glob(*patterns : Path | String, match_hidden = false, follow_symlinks = false, &block : String -> _)
¶
(*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.
.mkdir(path : Path | String, mode = 511) : Nil
¶
(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.
.mkdir_p(path : Path | String, mode = 511) : Nil
¶
(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).
.open(path : Path | String, & : self -> )
¶
(path : Path | String, & : self -> )
Opens a directory and yields it, closing it at the end of the block. Returns the value of the block.
Methods¶
#children : Array(String)
¶
: Array(String)
Returns an array containing all of the filenames except for .
and ..
in the given directory.
#each(& : String -> ) : Nil
¶
(& : 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
#each : Iterator(String)
¶
: Iterator(String)
Must return an Iterator
over the elements in this collection.
#each_child(& : String -> ) : Nil
¶
(& : 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
#entries : Array(String)
¶
: Array(String)
Returns an array containing all of the filenames in the given directory.
#inspect(io : IO) : Nil
¶
(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>
#read : String?
¶
: 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"]
#to_s(io : IO) : Nil
¶
(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>