Skip to content

Instantly share code, notes, and snippets.

@wycats
Created April 6, 2012 21:27
Show Gist options
  • Save wycats/2323084 to your computer and use it in GitHub Desktop.
Save wycats/2323084 to your computer and use it in GitHub Desktop.
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