【问题标题】:How can I get current depth of the Ruby interpreter stack?如何获得 Ruby 解释器堆栈的当前深度?
【发布时间】:2019-09-24 02:40:40
【问题描述】:

我想获取堆栈深度,以了解它在递归函数中的深度。

我找到了How do I get the current depth of the Python interpreter stack? 这个,这正是我想要的,但它是 python,当我用谷歌搜索“Ruby 获取堆栈深度”时,我找不到类似的东西。

我想做的是这样的:

def recursive_func(n)
   if n == 1
      return get_current_depth()
   end
   recursive_func(n - 1)
end

我可以使用什么?

【问题讨论】:

    标签: ruby stack


    【解决方案1】:

    使用caller 可能是这样的:

    caller.length
    

    请记住,当您的程序启动时,它从某个深度开始,因此您可能需要从计数中减去它。

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 2012-04-02
      • 2012-01-09
      相关资源
      最近更新 更多