Skip to content

Instantly share code, notes, and snippets.

@sevos
Created August 18, 2010 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevos/535253 to your computer and use it in GitHub Desktop.
Save sevos/535253 to your computer and use it in GitHub Desktop.
module Animal
def self.included(base)
class << base
def method_added(name)
unless (@wrapped_methods ||= []).include? name
@wrapped_methods.push name
puts "Defined #{name}"
wrap_method name
end
end
def wrap_method(method_name)
method = instance_method(method_name)
define_method(method_name) do |*args|
puts "#{method_name} called"
method.bind(self).call(*args)
end
end
end
[:miow].each {|m| base.wrap_method m if base.method_defined? m}
end
end
class Cat
def miow
puts "#{@name} miows"
end
include Animal
def initialize(name)
@name = name
end
def miow
puts "#{@name} miows"
end
end
Cat.new("Eric").miow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment