Created
April 6, 2012 21:27
-
-
Save wycats/2323084 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Proxy | |
def initialize(obj, modules) | |
@obj = obj | |
modules.each do |mod| | |
extend mod | |
end | |
end | |
def method_missing(meth, *args, &block) | |
@obj.send(meth, *args, &block).tap do |ret| | |
return self if ret == @obj | |
end | |
end | |
end | |
class Object | |
def with_aspects(*modules) | |
proxy = Proxy.new(self, modules) | |
end | |
alias with_aspect with_aspects | |
end | |
class Person | |
def initialize(name) | |
@name = name | |
end | |
def hello | |
"#{@name} says hello" | |
end | |
def myself | |
self | |
end | |
end | |
module Yeller | |
def hello | |
super.upcase | |
end | |
end | |
yehuda = Person.new("Yehuda") | |
p yehuda.with_aspect(Yeller).hello | |
p yehuda.with_aspect(Yeller).myself.hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment