class Dir
def self.remove_empty_directories(dir)
def self.remove_empty_directories(dir) # List all entries in the directory except for '.' and '..' Dir.entries(dir).each do |entry| next if entry == "." || entry == ".." # Skip the current and parent directory entries path = File.join(dir, entry) # Construct the full path if File.directory?(path) remove_empty_directories(path) # Recursively call the method if the entry is a directory # Remove the directory if it's empty after processing its contents Dir.rmdir(path) if Dir.empty?(path) end end rescue Errno::ENOENT # Handle the case where the directory doesn't exist or is removed before rmdir is called puts "Directory not found: #{dir}" end