Skip to content

Instantly share code, notes, and snippets.

@slawosz
Created May 20, 2011 12:00
Show Gist options
  • Save slawosz/982783 to your computer and use it in GitHub Desktop.
Save slawosz/982783 to your computer and use it in GitHub Desktop.
Playing with modules with extend
class Module
def method_added(name)
p "#{name},#{caller[0..1]}"
end
end
class TestClass
def foo
p 'pure foo'
end
end
module SetProcs
def self.extended(base)
base.class_eval do
# dont works with include InstanceMethods
def foo
p 'foo 2'
self.class._procs.shift.call
super
end
def bar
p 'from module'
end
end
base.instance_eval do
def set_procs(*procs)
@_procs = []
procs.each do |_proc|
@_procs << _proc
end
end
def self._procs
@_procs
end
end
end
end
module InstanceMethods
def foo
p 'foo 2'
self.class._procs.shift.call
super
end
def bar
p 'from module'
end
end
TestClass.extend(SetProcs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment