Skip to content

Instantly share code, notes, and snippets.

@abriening
Created November 7, 2012 16:33
Show Gist options
  • Save abriening/4032656 to your computer and use it in GitHub Desktop.
Save abriening/4032656 to your computer and use it in GitHub Desktop.
require "delegate"
class ObjectDelegator < SimpleDelegator
undef_method :class, :object_id
end
def Object.delegate(object, delegation)
ObjectDelegator.new(object).tap{|x| Array(delegation).each{|m| x.extend m } }
end
module Greeter
def announce
"this is the #{self.class} #{self.name}"
end
end
module UpcasedGreeter
def announce
super.upcase
end
end
class Person
attr_accessor :name
end
p = Person.new
p.name = "George"
g = Object.delegate p, Greeter
puts g.announce
ug = Object.delegate g, UpcasedGreeter
puts ug.announce
ugg = Object.delegate p, [Greeter, UpcasedGreeter]
puts ugg.announce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment