Open In App

Ruby | Dir Class and its methods

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Dir.exist?"dir_name"

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.

Dir.pwd

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.

Dir.getwd

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

Dir.home

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.

Dir.glob(“pattern”)

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.

h_o.close

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.

each{|filename|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.

h_o.fileno

Below image illustrates the use of fileno method:

4. path : This method returns the path parameter.

h_o.path

Below image illustrates the use of path method:

5. pos : This method returns current position parameter.

h_o.pos=integer

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

h_o.read

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

h_o.tell

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

h_o.seek(integer)

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

h_o.rewind

Reference: https://ruby-doc.org/core-2.2.0/Dir.html
 


Last Updated : 18 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads