开发者

Recursive stack size

开发者 https://www.devze.com 2022-12-23 12:11 出处:网络
How i can know 开发者_运维知识库the current method stack frame while a recursive call in ruby?I have no idea why you would need that, but caller.size should do the job:

How i can know 开发者_运维知识库the current method stack frame while a recursive call in ruby?


I have no idea why you would need that, but caller.size should do the job:

def recurse(n)
  puts caller.size
  recurse(n-1) unless n <= 0
end

recurse(5)  # => Outputs 1 to 6

This works in Ruby 1.9, but there is apparently a bug in Ruby 1.8. Just filed it on redmine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号