Detecting recursive calls

recursion, ruby

Solution

Just increment a counter (an instance or global variable) after entry to your method and decrement it on exit.

At entry, the counter tells you your recursion level.

Problem

What is the best way to detect recursive method calls? Perhaps, it can be made into this format: ``` def foo if some_code_to_detect_recursive_call ... else ... end end ``` I can think of a way parsing the `caller` information, but I am interested in seeing if there are better ways.

Original source