Lucas
Lucas

Reputation: 3119

Recursive stack size

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

Upvotes: 1

Views: 413

Answers (1)

Marc-André Lafortune
Marc-André Lafortune

Reputation: 79562

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.

Upvotes: 3

Related Questions