Ruby objects can be built in a way such that they are easier to use/test/debug from the point of view that if you do not automatically run their intended functionality from the constructor, potential side-effects and bugs can be better examined.
In general, I find it helpful to consider a constructors' purpose constrained only to setting instance-level attributes based on the passed arguments or using pre-defined defaults. Anything outside of that, I would consider setting up a class-level factory method.
Consider creating semantically-named factory style class methods that create an object and then call a specific method.
I find this especially helpful for hand-rolled presenters and worker classes (e.g. for Resque).
Also, per feedback from ahoward, using class-level factory methods makes subclassing much easier to grok in terms of calling super
.
Since instances of BuilderA
will automatically have their build!
method called, they cannot be easily used from IRB.
Instances of BuilderB
can be instantiated and have their methods tested more atomically. Also, by stubbing the build!
method, you can easily test the class-level factory method.
Dig into Class::allocate re: overriding Class::new
. This is also a tip from ahoward.