Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Created April 26, 2012 09:11
Show Gist options
  • Save makaroni4/2497938 to your computer and use it in GitHub Desktop.
Save makaroni4/2497938 to your computer and use it in GitHub Desktop.
Wrap instance methods of object to log them
class User
def say
puts "Hello"
end
end
def override_instance_methods(c)
c.instance_methods(false).each do |m|
c.class_eval <<-RUBY
alias #{m}_original #{m}
def #{m}(*args, &block)
puts "Called #{c.name}##{m}"
#{m}_original(*args, &block)
end
RUBY
end
end
override_instance_methods User
User.new.say
# => Called User#say
# => Hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment