Open In App
Related Articles

Ruby | Dir Class and its methods

Improve Article
Save Article
Like Article

A directory is a place for storing files. In Ruby, directories are handled by the Dir class and files are handled by the File class. In directories double dot(..) denotes parent directory and single dot(.) denotes directory itself.

Class Methods

1. mkdir : This method is used to create a new directory. You can also put permission on the new directory. 

Dir.mkdir "dir_name", permission

2. Deleting directory : To delete a directory rmdir, delete, and unlink methods are used, the work of all these methods are same.

Dir.delete "dir_name"
Dir.rmdir "dir_name"
Dir.unlink " Dir_name"

3. exist? : By using exist? method you can check whether the directory exists or not. It returns value in true or false. 


Below image illustrates the use of mkdir, delete, and exist? methods:

4. pwd : To check the current working directory, pwd(present working directory) method is used.


5. chdir : To change the current working directory, chdir method is used. In this method, you can simply pass the path to the directory where you want to move.

Dir.chdir ”path”

The string parameter used in the chdir method is the absolute or relative path.

Below image illustrates the use of chdir and pwd methods:

6. entries : To check what a directory contains. It provides you with an array of content.

Dir.entries”dir_name” #[“.”, “..”, “file.txt”, “another directory”]

7. getwd : This method is used to return path of the current working directory.


8. home : This method is used to return home directory of current user.


Below image illustrates the use of entries, getwd, and home methods:

9. glob : This method is used to heck certain file in the current directory. It works on pattern matching concept. It expands pattern i.e array of pattern or a string pattern and returns the value as matched. Some notations used in glob method are:
*: It matches all files.
c*: It matches files starting with c. 
*c: It matches files ending with c.
*c*: It matches all the files with contains c in them including starting and ending.
**: It matches directories recursively.
?: it matches any one character.
[set]: It matches any one character in the set.
{p, q}: It matches either p or either q literal.
\: It escapes the next metacharacter.


Below image illustrates how to use the glob method:

Instance Methods

Here h_o is the object of dir class.

1. close : It is used to close the directory stream.


Below image illustrates the use of close method:

2. each : In each method, the block should be called once for each entry in the directory and pass the filename for each entry as a parameter to the block.


Below image illustrates the use of each method:

3. fileno : This method is used to provide the file number used in dir or we can say it provides the description of the file in integer value.


Below image illustrates the use of fileno method:

4. path : This method returns the path parameter.


Below image illustrates the use of path method:

5. pos : This method returns current position parameter.


6. read : This method reads the next entry from the dir and returns as a string.

7. tell : This method is used to tell the current position in dir


8. seek : This method is used to seek the specific location in dir. It returns value in the form of integer.

9. Rewind : This method is used to reposition dir to the first position.



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 18 Nov, 2021
Like Article
Save Article
Similar Reads