Class Dir has directory streams as objects which represents directories in underlying file system.
Directories are handled with Dir class.
Creating a Directory
To create a directory mkdir command is used. You can give permission to a directory if you want.
Syntax:
Dir.mkdir "dirName" , permission
Example:
Dir.mkdir "project"
We have created a directory “project” in out system.
Checking a Directory exists or not
To check whether a directory exists or not exists? Method is used.
Syntax:
puts Dir.exists? "dirName"
Example:
#!/usr/bin/ruby
puts Dir.exists? "project"
puts Dir.exists? "pproject"
Output:

the correct directory name display true and wrong directory name display false.
Current Working Directory
To know the current working directory pwd method is used.
Syntax:
puts Dir.pwd
Example:
puts Dir.pwd
Output:

Removing Directory
To remove a directory, rmdir, unlink or delete methods are used. They perform same function for a Ruby directory.
Syntax:
Dir.rmdir "dirName"
Example:
#!/usr/bin/ruby
Dir.rmdir "project"
puts Dir.exists? "project"
Output:

Leave a Reply