imagelogo

Durkheim's Blog


DBC Phase 0, Week 8


Recursion

February 15, 2015

As part of our experience at DBC (and our future experience as developers), we'll often be asked to research and learn new concepts or even languages. Previously, we were instructed not to use recursion to solve any of our challenges, but now we've been given an opportunity to research new concepts, and I thought this would be a good opportunity to cover recursion.

In Ruby, recursion is the act of calling a method within a method. As an example:

def countdown(integer)
return if integer.zero?
puts integer
countdown(integer-1)
end

countdown(5)
5
4
3
2
1

In the above code, we set a base case of if integer.zero?. We then puts the integer use the countdown method within itself to puts integers down to 1.

|