Ruby Dir class and method

Dir is a directory stream that represents the name of a file in the directory in the operating system. The Dir class also has directory-related operations, such as wildcard file name matching, changing the working directory, and so on.

class method

serial numbermethod & description
Dir::glob( pat)

returns an array containing the filenames that match the specified wildcard pattern pat :
  • * - Matches any string containing a null string
  • ** - Recursively match any string
  • ? - Match any single character
  • [...] - Match any of the enclosed characters
  • {a,b...} - Match any of the strings
Dir["foo.*"] # matches "foo.c", "foo.rb", etc.
Dir["foo.?"] # matches "foo.c", "foo.h", etc.
2Dir::chdir( path)
Change the current directory.
3Dir::chroot( path)
Change the root directory (superusers only). Not available on all platforms.
4Dir::delete( path)
Delete the directory specified by path. The directory must be empty.
5Dir::entries( path)
returns an array containing the filenames in the directory path.
6Dir::foreach( path) {| f| ...}
Execute each file in the directory specified by path One block.

returns the current directory.
8Dir::mkdir( path[, mode=0777])
Create the directory specified by path. Permission mode can be modified by the value of File::umask and will be ignored on Win32 platforms.
9Dir::new( path)
Dir::open( path)
Dir::open( path) {| dir| ...}

returns a new directory object for path . If open gives a block, the new directory object is passed to the block, and the block closes the directory object before terminating.
See Dir::getwd.
11Dir::rmdir( path)
Dir::unlink( path)
Dir::delete( path)

Delete the directory specified by path. The directory must be empty.

Instance method

Assume that d is an instance of the Dir class:

serial numbermethod & description
Close the directory stream.
2d.each {| f| ...}
Executes a block for each entry in d.
returns the current position in d.
Set the location in the directory stream.
5d.pos= pos

moves to a location in d. Pos must be a value returned by d.pos or 0.
Returns the next entry for d.
Move the position in d to the first entry.
8d.seek(po s)
See d.pos=pos.
See d.pos.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.