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 (.).
Alias for .chdir.
Changes the current working directory of the process to the given string and invokes the block, restoring the original working directory when the block exists.
Changes the current working directory of the process to the given string.
Returns an array containing all of the filenames in the given directory.
Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
Alias for .new(path)
Opens a directory and yields it, closing it at the end of the block.
Returns a new directory object for the named directory.
Closes the directory stream.
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
Reads the next entry from dir and returns it as a string.
Repositions this directory to the first entry.
Alias for .chdir.
Changes the current working directory of the process to the given string and invokes the block, restoring the original working directory when the block exists.
Changes the current working directory of the process to the given string.
Returns an array containing all of the filenames in the given directory.
Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
Alias for .new(path)
Opens a directory and yields it, closing it at the end of the block. Returns the value of the block.
Returns a new directory object for the named directory.
Closes the directory stream.
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
d = Dir.new("testdir")
d.each {|x| puts "Got #{x}" }
produces:
Got .
Got ..
Got config.h
Got main.rbReads the next entry from dir and returns it as a string. Returns nil at the end of the stream.
d = Dir.new("testdir")
d.read #=> "."
d.read #=> ".."
d.read #=> "config.h"Repositions this directory to the first entry.