Skip to content

Instantly share code, notes, and snippets.

@ipmsteven
Last active November 13, 2015 08:41
Show Gist options
  • Select an option

  • Save ipmsteven/32e25cb002f2ea4de866 to your computer and use it in GitHub Desktop.

Select an option

Save ipmsteven/32e25cb002f2ea4de866 to your computer and use it in GitHub Desktop.
Ruby MultipleInitializers
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