0

I want to get stack depth, to know how deep it go in recursive function.

I found How do I get the current depth of the Python interpreter stack? this, and this is what exactly I want, but it's python, and I couldn't find anything like this when I googled 'Ruby get stack depth'.

What I want to do is something like this:

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

What can I use?

LegenDUST
  • 142
  • 9

1 Answers1

3

Probably like this using caller:

caller.length

Keep in mind that starts at a certain depth when your program spins up, so you may want to subtract that from your count.

tadman
  • 208,517
  • 23
  • 234
  • 262