Recursive Call Detection

What is the best way to detect recursive method calls? Perhaps it can be done in this format:

def foo
  if some_code_to_detect_recursive_call
    ...
  else
    ...
  end
end

      

I can think of a way to parse the information caller

, but I'm curious to see if there are better ways.

+3


source to share


1 answer


Just add a counter (instance or global variable) after entering your method and decrement it on exit.



On entry, the counter tells you your recursion level.

+3


source







All Articles