If you’re new to ruby, then you may be tempted to implement private class methods like this:
In this example, we have a
MachineBuilder that exposes two class
build_laser. These methods delegate their
work to other methods. Our intent is to expose a minimal interface to
MachineBuilder, so we want to hide how it actually
builds printers and lasers.
We try using the
private keyword to change the visibility of our
assemble. This does not
work. These three methods are still publicly visible.
Why Is This Approach Broken?
private, is a
It sets the visibility for “subsequent methods” to private. The method
applies to instance methods, not class methods.
This implementation works by adding class methods and then
Module to declare
the desired methods as private.
This alternative implementation works by adding private instance methods to
MachineBuilder’s eigenclass (a.k.a. singleton or meta class).
The difference between instance and class methods will be covered in a different post.