views:

107

answers:

1

How i can know the current method stack frame while a recursive call in ruby?

+1  A: 

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.

Marc-André Lafortune