检测递归调用(Detecting recursive calls)
检测递归方法调用的最佳方法是什么? 也许,它可以制成这种格式:
def foo if some_code_to_detect_recursive_call ... else ... end end我可以想一种解析caller信息的方法,但我有兴趣看看是否有更好的方法。
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 endI can think of a way parsing the caller information, but I am interested in seeing if there are better ways.
最满意答案
只需在输入方法后递增计数器(实例或全局变量),然后在退出时递减计数器。
在输入时,计数器会告诉您递归级别。
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.
更多推荐
发布评论