Dissecting Code With Ruby's Caller Method
Here is a short snippet of code that has become one of my favourite debugging tools recently. It’s not efficient, definitely not elegant, but man is it useful.
location = caller.first
location = location[0...location.rindex(":")]
line_number = location[(location.rindex(":")+1)..-1]
file = location[0...location.rindex(":")]
puts location
puts File.open(file).readlines[line_number.to_i - 1]
puts "#{values}"
puts "--"*20But let’s back up a moment, how did I get to this stage?
Background
As part of consultancy work we often inherit applications developed by other Rails shops of varying quality. Previous developers of the code we receive have often used, well, let’s just say “creative” approaches.