On 5/24/06, Victor Shepelev <vshepelev / imho.com.ua> wrote:
> Here can help my Module#add_tracer, described here
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192291

That's a nice idea. I made a small modification to count the totals of
each of the calling methods instead of printing each time:

$traces = Hash.new(0)

class Module
  def add_tracer(meth)
    m_alias = case meth
      when :[] : "old_idx"
      when :+ : "old_plus"
      when :- : "old_minus"
      #and so on
      else ; "old_#{meth}"
    end
    module_eval %Q{
      alias :#{m_alias} :#{meth}
      def #{meth}(*arg, &block)
        str = caller[0]+"-"+self.class.name+"-##{meth}"
        $traces[str] += 1
        #{m_alias}(*arg, &block)
      end
    }
  end
end

Array.add_tracer(:each)

....
Your code here
....

$traces.each {|name, value| puts "#{name} - #{value} calls"}

You can also sort it by the value in the hash to get an ordered list.
This will do for a hack but writing a tracing gem would be great.

Pedro.