What’s wrong with this Ruby code?

A descent into recursive programming

Darren Broemmer
CodeX

--

Photo by Yousef Espanioly on Unsplash

Back by popular demand, welcome to another edition of America’s favorite game show, What’s Wrong with this Code? In this series, we examine seemingly reasonable code and try to determine why a computer might think otherwise.

Along the way, we also tackle key existential questions such as when is zero not zero. See our first article in the series for insight and fun regarding floating-point numbers and digits of precision for zero.

When I studied computer science many moons ago, my first programming course quickly introduced the idea of recursion. I always thought this was an odd place to begin because recursion is not an easy concept to grasp, especially for beginners. Sometimes, it’s not so intuitive for experienced folks either.

It does have tremendous benefits though when there are high levels of branching, in particular, when you are dealing with tree or graph data structures. I might go so far as to say it’s essential for those use cases. More on that later.

--

--

Darren Broemmer
CodeX
Writer for

I write weekly on puzzles, science, and technology. Technologist, published author, ex-BigTech, indie publisher.