Last active
November 13, 2015 08:41
-
-
Save ipmsteven/32e25cb002f2ea4de866 to your computer and use it in GitHub Desktop.
Ruby MultipleInitializers
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
| module MultipleInitializers | |
| def new_using(method, *args) | |
| instance = self.allocate | |
| raise 'implement me' unless instance.respond_to?(method.to_sym, true) | |
| instance.send(method.to_sym, *args) | |
| instance | |
| end | |
| end | |
| class Person | |
| extend MultipleInitializers | |
| def initialize_from_brain(brain) | |
| puts 'hehe' | |
| end | |
| private | |
| def initialize_from_organs(organs) | |
| puts 'xixi' | |
| end | |
| end | |
| brain = Object.new | |
| person = Person.new_using(:initialize_from_brain, brain) | |
| organs = Object.new | |
| person = Person.new_using(:initialize_from_organs, organs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment