Last active
December 16, 2015 02:09
-
-
Save ajsharp/5360447 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
| 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