I've defined this method on ruby ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
and (Rails 4.2.5.2)
def log_method_backtrace
backtrace = []
(4..8).map do |i| # 4 because before is ... map, log_method_backtrace...
b = caller[i][/\/(?:.(?!\/))+$/]
b = b[1..-2] # *This is the error line
b = b.sub(':in `', '#')
backtrace << "#{b} <- "
end
log "(Method called from #{backtrace.join})"
end
When I call it it throw this error:
NoMethodError: undefined method `[]' for nil:NilClass from (pry):5:in `block in log_method_backtrace'
But if I place a debugger breakpoint (I'm using binding.pry) in that line, and run the same line it works.