Find those puts's

Published May 5, 2009 in Ruby

Here’s some code I hacked out when I was trying to find some debug standard output I had left in a Rails application. If you set Kernel.trace_output = true, output methods will print their calling line to make for easy cleanup. This also makes pp, p, and puts return whatever was passed to them, making it easy to add a debugging line without changing behavior

require 'pp'
module Kernel
  mattr_accessor :trace_output  
  %w(pp p puts).each do |method|
    define_method :"#{method}_with_source_and_passthrough" do |*args|
      puts_without_source_and_passthrough caller.first if Kernel.trace_output
      send :"#{method}_without_source_and_passthrough", *args
      puts_without_source_and_passthrough if Kernel.trace_output
      return *args
    end
    alias_method_chain method.to_sym, :source_and_passthrough
  end
end