Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment