Created
May 20, 2011 12:00
-
-
Save slawosz/982783 to your computer and use it in GitHub Desktop.
Playing with modules with extend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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