Skip to content

Instantly share code, notes, and snippets.

@langalex
Created February 3, 2013 17:35
Show Gist options
  • Save langalex/4702735 to your computer and use it in GitHub Desktop.
Save langalex/4702735 to your computer and use it in GitHub Desktop.
Extend an object with this module to print out all method calls to it.
module LogCalls
def self.extended(b)
b.class_eval do
(b.methods - Object.methods).each do |m|
define_method "#{m.to_s.gsub(/[\?\!=]/, '')}_with_logging#{m.to_s[/[\?\!=]/]}" do |*args|
puts m
send "#{m.to_s.gsub(/[\?\!=]/, '')}_without_logging#{m.to_s[/[\?\!=]/]}", *args
end
alias_method_chain m.to_sym, :logging
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment