Skip to content

Instantly share code, notes, and snippets.

@ajsharp
Last active December 16, 2015 02:09
Show Gist options
  • Select an option

  • Save ajsharp/5360447 to your computer and use it in GitHub Desktop.

Select an option

Save ajsharp/5360447 to your computer and use it in GitHub Desktop.
require 'forwardable'
module Things
def name
@name
end
end
class Blah
def name
'hey from blah!'
end
end
class Person
extend Forwardable
attr_accessor :name
def initialize(name, target=nil)
@name = name
@target = target
end
def init_delegators
(class << self; self; end).def_delegators :@target, :name
end
end
person = Person.new('alex', Blah.new)
person.name # => "alex"
person.init_delegators
person.name # => "hey from blah!"
Person.new('bob').name # => "bob"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment