Recursive stack size
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.
精彩评论