Home »
Ruby »
Ruby Programs
Ruby program to delete an empty directory using Dir::unlink(), Dir::delete() methods
Ruby Example: Write a program to delete an empty directory using Dir::unlink(), Dir::delete() methods.
Submitted by Nidhi, on February 21, 2022
Problem Solution:
In this program, we will remove the empty directories using Dir::unlink(), Dir::delete() methods. Then we will check given directories are removed or not using Dir::exists() method.
Program/Source Code:
The source code to delete an empty directory using Dir::unlink(), Dir::delete() methods is given below. The given program is compiled and executed on Windows 10 Operating System successfully.
# Ruby program to delete an empty directory
# using Dir::unlink(), Dir::delete()
# methods
puts Dir::exists?("MyDir1");
puts Dir::exists?("MyDir2");
Dir::unlink("MyDir1");
Dir::delete("MyDir2");
puts Dir::exists?("MyDir1");
puts Dir::exists?("MyDir2");
Output:
true
true
false
false
Explanation:
In the above program, we removed the MyDir1, MyDir2 directories using Dir::unlink(), Dir::delete() methods. Then we checked given directories have been deleted or not using the Dir::exists() method. If directories have been deleted successfully then it will print "true" otherwise it will print "false".
Ruby Directories Programs »