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:

https://gist.github.com/gangelo/5551902

if want call private (or protected) method outside of class, can use send. in factory have like:

instance = classa.send(:create) 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -