Skip to content

Instantly share code, notes, and snippets.

@kwstannard
Created November 14, 2012 15:44
Show Gist options
  • Save kwstannard/4072865 to your computer and use it in GitHub Desktop.
Save kwstannard/4072865 to your computer and use it in GitHub Desktop.
arg = `echo $COUNT_CALLS_TO`
KLASS, METHOD = *arg.split('#').map(&:strip)
module MethodCounter
module ClassMethods
@@method_times_run_var = 0
def method_times_called
@@method_times_run_var
end
def method_times_called=(inc)
@@method_times_run_var = inc
end
end
def self.included(base)
base.extend ClassMethods
base.class_eval do
alias_method "original_#{METHOD}", METHOD
define_method METHOD do
self.class.method_times_called += 1
send "original_#{METHOD}"
end
end
end
end
begin
Module.const_get(KLASS).send :include, MethodCounter
rescue NameError
class Class
alias_method :original_new, :new
def new(*args, &block)
include MethodCounter if self.name == KLASS && !self.include?(MethodCounter)
original_new *args, &block
end
end
end
at_exit do
puts "#{KLASS}##{METHOD} called #{Module.const_get(KLASS).method_times_called} times."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment