Now that you’ve seen how recursion works, we can use it to solve certain problems that would otherwise be uncrackable.

One type of problem in which recursion is a natural fit is when we need to delve into multiple layers of a problem without knowing how many layers there are.

Take the example of traversing through a filesystem. Let’s say you have a script that does something with all the contents inside a directory, such as printing all the subdirectory names. However, you don’t want the script to only handle the immediate subdirectories—you want it to act on all the subdirectories within the subdirectories of the directory and all of their subdirectories, and so on.

Let’s create a simple Ruby script that prints the names of all subdirectories within a given directory:

​  ​ def ​ ​ find_directories ​(directory)
​  ​ # Inspect each file within the directory. Some of these "files" may ​
​  ​ # actually be subdirectories. ​
​  Dir.​ foreach ​(directory) ​ do ​ |filename|
​  
​  ​ # If the current file is itself a subdirectory: ​
​  ​ if ​ File.​ directory? ​(​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​) &&
​  filename != ​ "." ​ && filename != ​ ".." ​
​  
​  ​ # Print out the full path name: ​
​  puts ​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​
​  ​ end ​
​  ​ end ​
​  ​ end ​

We can call this function by passing in a directory name. If we want to call it on the current directory, we could write:

​  find_directories(​ "." ​)

In this script, we look through each file within the given directory. If the file is itself a subdirectory (and isn’t a single or double period, which represent the current and previous directories, respectively), we print the subdirectory name.

While this works well, it only prints the names of the subdirectories immediately within the current directory. It does not print the names of the subdirectories within those subdirectories.

Let’s update our script so that it can search one level deeper:

​  ​ def ​ ​ find_directories ​(directory)
​  ​ # Loop through first-level directory: ​
​  Dir.​ foreach ​(directory) ​ do ​ |filename|
​  ​ if ​ File.​ directory? ​(​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​) &&
​  filename != ​ "." ​ && filename != ​ ".." ​
​  puts ​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​
​  
​  ​ # Loop through second-level subdirectory: ​
​  Dir.​ foreach ​(​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​) ​ do ​ |inner_filename|
​  ​ if ​ File.​ directory? ​(​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ / ​​ #{ ​inner_filename​ } ​​ " ​) &&
​  inner_filename != ​ "." ​ && inner_filename != ​ ".." ​
​  puts ​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ / ​​ #{ ​inner_filename​ } ​​ " ​
​  ​ end ​
​  ​ end ​
​  
​  ​ end ​
​  ​ end ​
​  ​ end ​

Now, every time our script discovers a directory, it then conducts an identical loop through the subdirectories of that directory and prints the names of the subdirectories. But this script also has its limitations because it’s only searching two levels deep. What if we want to search three, four, or five levels deep? We would need five levels of nested loops.

And what if we want to search as deep as our subdirectories go? That would seem to be impossible, as we don’t even know how many levels there are.

And this is where recursion really shines. With recursion, we can write a script that goes arbitrarily deep—and is also simple!

​  ​ def ​ ​ find_directories ​(directory)
​  Dir.​ foreach ​(directory) ​ do ​ |filename|
​  ​ if ​ File.​ directory? ​(​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​) &&
​  filename != ​ "." ​ && filename != ​ ".." ​
​  puts ​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​
​  
​  ​ # Recursively call this function on the subdirectory: ​
​  find_directories(​ " ​​ #{ ​directory​ } ​​ / ​​ #{ ​filename​ } ​​ " ​)
​  ​ end ​
​  ​ end ​
​  ​ end ​

As this script encounters files that are themselves subdirectories, it calls the find_directories method upon that very subdirectory. The script can therefore dig as deep as it needs to, leaving no subdirectory unturned.

To visualize how this algorithm acts on an example filesystem, examine the diagram , which specifies the order in which the script traverses the subdirectories.

images/recursively_recurse_with_recursion/filesystem.png

We’ll encounter this process again with a detailed visual walk-through in ​ Depth-First Search ​ .