Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Dir Class and Methods

Ruby Dir Class and Methods

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

Class Methods

No. Method & Description
1 **Dir[pat]

Dir::glob( pat)**
Returns an array containing file names that match the specified wildcard pattern pat:

Dir["foo.*"] # matches "foo.c", "foo.rb", etc. Dir["foo.?"] # matches "foo.c", "foo.h", etc. | | 2 | **Dir::chdir( path)** Changes the current directory. | | 3 | **Dir::chroot( path)** Changes the root directory (superuser only). Not available on all platforms. | | 4 | **Dir::delete( path)** Deletes the directory specified by path. The directory must be empty. | | 5 | **Dir::entries( path)** Returns an array containing the file names in the directory path. | | 6 | **Dir::foreach( path) {\| f\| ...}** Executes the block once for each file in the directory specified by path. | | 7 | **Dir::getwd Dir::pwd** Returns the current directory. | | 8 | **Dir::mkdir( path[, mode=0777])** Creates the directory specified by path. The permission mode can be modified by the value of File::umask and is ignored on Win32 platforms. | | 9 | **Dir::new( path) Dir::open( path) Dir::open( path) {\| dir\| ...}** Returns a new directory object for path. If open is given a block, the new directory object is passed to the block, and the block closes the directory object before terminating. | | 10 | **Dir::pwd** See Dir::getwd. | | 11 | **Dir::rmdir( path) Dir::unlink( path) Dir::delete( path)** Deletes the directory specified by path. The directory must be empty. |

Instance Methods

Assume d Is Dir An instance of the class:

No. Method & Description
1 d.close
Closes the directory stream.
2 d.each {| f| ...}
Executes the block once for each entry in d.
3 d.pos
 d.tell       
 返回 d 中的当前位置。 |

| 4 | d.pos= offset
Sets the position in the directory stream. | | 5 | d.pos= pos
d.seek(pos)

Moves to a certain position in d. pos must be a value returned by d.pos or 0. | | 6 | d.read
Returns the next entry in d. | | 7 | d.rewind
Moves the position in d to the first entry. | | 8 | d.seek(po s)
See d.pos=pos. | | 9 | d.tell
See d.pos. |