Ruby | Dir Class and its methods

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

  • mkdir : This method is used to create a new directory. You can also put permission on the new directory.
    Dir.mkdir "dir_name", permission
  • 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"
  • 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 illustrate the use of mkdir, delete, and exist? methods:

  • pwd : To check the current working directory, pwd(present working directory) method is used.
    Dir.pwd
  • 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 illustrate the use of chdir and pwd methods:

  • entries : To check what a directory contains. It provides you with an array of the content.
    Dir.entries”dir_name” #[“.”, “..”, “file.txt”, “another directory”]
  • getwd : This method is used to return path of the current working directory.
    Dir.getwd
  • home : This method is used to return home directory of current user.
    Dir.home

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

  • glob : This method is used to heck certain file in the current directory. It works on pattern matching concept. It expends 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 imgae illustrate how to use the glob method:

  • Instance Methods

    Here h_o is the object of dir class.

  • close : It is used to close the directory stream.
    h_o.close

    Below image illustrate the use of close method:

  • 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 illustrate the use of each method:

  • 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 illustrate the use of fileno method:

  • path : This method returns the path parameter.
    h_o.path

    Below image illustrate the use of path method:

  • pos : This method method returns current position parameter.
    h_o.pos=integer
  • read : This method reads the next entry from the dir and returns as a string.
    h_o.read
  • tell : This method is used to tell the current position in dir.
    h_o.tell
  • seek : This method is used to seek the specific location in dir. It returns value in the form of integer.
    h_o.seek(integer)
  • 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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.