Recursion in Ruby
A method that calls itself
Recursion is a method that solves a small case directly and the rest by calling itself on something smaller. The factorial is the standard example, and the base case is the line that stops it:
def fact(n) return 1 if n < 2 n * fact(n - 1) end puts fact(5)
ruby -e 'def fact(n) = n < 2 ? 1 : n * fact(n - 1); puts fact(5)'
120
The call stack holds one frame per step: fact(5) waits for fact(4), which waits for fact(3), and the multiplication happens on the way back. A recursion without a base case, or with one that is never reached, ends in the stack error from the last section.
Recursive data
Recursion is at its best when the data is already nested. An array that may contain arrays is summed by asking whether the item is an array:
ruby -e 'def total(x) = x.is_a?(Array) ? x.sum { total(_1) } : x; p total([1, [2, [3]]])'
6
_1 is the block parameter Ruby 3 provides, so the recursive call fits on one line. The same shape reads a JSON tree, a comment thread with replies, or an expression tree — any structure where a node holds nodes.
The iterative version keeps an explicit stack and is the one to write when the data can be deep. Ruby's own Array#flatten is the special case of this recursion, written in C and without a Ruby frame per level.
Walking a directory
A file tree is the everyday recursion, and Dir.children plus File.directory? (both from lesson 8.3, later) is all it needs. The program builds the folder it walks, so it runs on its own, and it prints the tree before the count:
require "fileutils" def count_files(dir) total = 0 Dir.children(dir).each do |name| path = File.join(dir, name) total += File.directory?(path) ? count_files(path) : 1 end total end def show(dir, prefix = "") entries = Dir.children(dir).sort entries.each do |name| last = name == entries.last puts "#{prefix}#{last ? "└── " : "├── "}#{name}" path = File.join(dir, name) show(path, prefix + (last ? " " : "│ ")) if File.directory?(path) end end FileUtils.mkdir_p("walk/a/b") FileUtils.mkdir_p("walk/c") %w[walk/a/b/three.txt walk/a/two.txt walk/one.txt].each { |f| File.write(f, "") } puts "walk" show("walk") puts count_files("walk")
walk ├── a │ ├── b │ │ └── three.txt │ └── two.txt ├── c └── one.txt
ruby walk.rb
3
The folder c is empty and contributes nothing, which is the one case to remember: a directory with no children returns zero without needing a special branch.
The stack has a limit
Each call takes a frame, and the frames are finite. A hundred thousand levels is past the limit on a default Ruby:
ruby -e 'def down(n) = n.zero? ? 0 : down(n - 1); down(100_000)'
-e:1:in 'Object#down': stack level too deep (SystemStackError)
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
... 10908 levels...
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in 'Object#down'
from -e:1:in '<main>'
Ruby prints the first frames, a count, and the last ones, because
printing every frame would be useless. The limit is around ten thousand
Ruby frames by default; RUBY_THREAD_VM_STACK_SIZE
changes it, and a deep recursion is still better rewritten as a loop.
Ruby does not optimise a tail call, so a recursive method that returns
the call directly still grows the stack. The loop version of
down is
while n > 0; n -= 1; end, which uses one
frame.
What to watch for
Write the base case first and test it alone. A recursion that fails on
the smallest input is a base case that never matches, not a problem with
the recursive step.
A recursion over a structure that a user controls can be turned into a
denial of service: a JSON document nested ten thousand levels deep
exhausts the stack. Validate the depth, or walk it with an explicit
stack.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a recursive method need besides the call to itself?
Answer
A base case that answers without another call
Without a base case that is reached, the calls never stop and the stack fills.
-
What does this program print?
def fact(n) return 1 if n < 2 n * fact(n - 1) end puts fact(5)Answer
120
The multiplications happen on the way back: 5 * 4 * 3 * 2 * 1.
-
What does this program print?
def total(x) x.is_a?(Array) ? x.sum { total(_1) } : x end p total([1, [2, [3]]])Answer
6
Each nested array is summed by the same method, so the numbers at every level add up.
-
A directory contains no files and no subdirectories. What does a recursive file count return for it?
Answer
0, without needing a special branch
The loop over no children adds nothing, so the total stays 0.
-
What does this program print?
def fact(n) return 1 if n < 2 n * fact(n - 1) end puts fact(1)Answer
1
The base case is reached immediately and answers 1.
-
What happens to a recursion with no reachable base case?
Answer
It runs until the stack is full and raises SystemStackError
Every call takes a frame, and the frames are finite.
-
What does this program raise?
def down(n) = n.zero? ? 0 : down(n - 1) down(100_000)Answer
SystemStackError: stack level too deep
A hundred thousand frames is past the default limit of about ten thousand.
-
Does Ruby optimise a tail call?
Answer
No: the recursive method that returns the call directly still grows the stack
That is why a deep recursion is better rewritten as a loop.
-
A JSON document can be nested thousands of levels deep by a user. What is the risk of walking it recursively?
Answer
SystemStackError, which turns the input into a denial of service
The lesson suggests validating the depth or walking it with an explicit stack.
-
What does
Array#flattenhave to do with this lesson?Answer
It is the same recursion written in C, without a Ruby frame per level
The special case of flattening nested arrays is implemented in C, so deep data costs no Ruby frames.
Article author: Arthur Isaev