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.
calleris a native Ruby method that returns the current execution stack as an array of strings. The problem I was having was because, as my code was running in the console one of the lines was:All the other lines had a filename/line/method name format as:
The regular expresion that I was using
[/\/(?:.(?!\/))+$/]was expected to capture the filename as the line had no a filename it returnedniland theb[-1..2]was throwing an error asbwas `nil.While I'm debugging the
callervariable got replaced on thebinding.pryexecution time, making the code work.This is how I fixed the code to make it work: