I want my code to do the following:
def debug (message)
puts message.rjust(message.length + (stack_trace-1 * 2)) # -1 for the call to debug
end
def a
debug "in a"
b
debug "end a"
end
def b
debug "in b"
c
debug "end b"
end
def c
debug "in c"
meaningful_work
debug "end c"
end
Output:
in a
in b
in c
end c
end b
end a
I've done this before in PHP, like this:
function echon($string){
$nest_level = count(debug_backtrace()) - 1; // minus one to ignore the call to *this* function
echo str_repeat(" ", $nest_level) . $string . "\n";
}
As far as my google-fu can tell, there isn't a Ruby equivalent to debug_backtrace
. I don't want to modify the calls of all the numerous methods in the code base I am working in to pass along a backtrace - that is needlessly cumbersome and hard to revert.
Is there some global I can use to keep track here?