class Dir

Overview

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 (.).

Superclass hierarchy

Object
Reference
Dir

Included Modules

Enumerable(String), Iterable

Defined in:

Class Method Summary

Instance Method Summary

Class Method Detail

def self.[](patterns : Enumerable(String))

def self.[](*patterns)

def self.cd(path)

Alias for .chdir.


def self.chdir(path, &block)

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.


def self.chdir(path)

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


def self.entries(dirname)

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


def self.exists?(path)

def self.foreach(dirname, &block)

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


def self.glob(*patterns)

def self.glob(patterns : Enumerable(String), &block)

def self.glob(*patterns, &block)

def self.glob(patterns : Enumerable(String))

def self.mkdir(path, mode = 511)

def self.mkdir_p(path, mode = 511)

def self.open(path)

Alias for .new(path)


def self.open(path, &block)

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


def self.rmdir(path)

def self.working_directory

def self.new(path)

Returns a new directory object for the named directory.


Instance Method Detail

def close

Closes the directory stream.


def each

def each(&block)

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.rb

def path

def read

Reads 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"

def rewind

Repositions this directory to the first entry.


def to_s(io)