A method that calls itself. The only time I ever saw something calling itself was outside of my wonderful apartment in the delightfully urine soaked street underneath a blanket. I could not for the life of me grasp how it worked. Yeah, I got the metaphors.
“It’s kind of like Inception. You can’t wake out of the dream unless you wake out of all the dreams.”
“It’s like an accordion that stretches out and has to fold back in.”
There were many, many real world examples. I get the concept. I just didn’t get it in code. Until I did pseudocode and worked the thing out by hand.
Here’s the one lined code for figuring out what fibonacci number is at n position:
def fibonacci(n) n < 2 ? n : fibonacci(n-1) + fibonacci(n-2) end
It is not as overwhelming as it looks. The best explanation is that until the method meets the condition n > 2, it will continue subtracting n’s until it reaches n(1), a value which it already knows (as well as n(0)). Looking at the diagram, it all makes sense.