metaprogramming - Want to instantiate a ruby class using a private constructor from factory -
i used c#/.net, come form typesafe background. using ruby. want create class (classa) has private (i'd settle protected if need to) constructor. reason being, want create factory (factorymodule::create) controls instantiation of classa objects. realize not fool-proof(?) in ruby, @ least code self-documenting in obvious based on code , tests classa must instantiated via factorymodule::create method. in modulefactory::create method, tried changing visibility of classa constructor public, instantiating object, changing visibility private a) received errors , b) sloppy , not thread-safe. thoughts?
update answer:
if want call private (or protected) method outside of class, can use send
. in factory have like:
instance = classa.send(:create)
Comments
Post a Comment