Specifying Access Control

When designing the interface for a class, it’s important to consider how much of your class you’ll expose to the outside world. Allow too much access into your class, and you risk increasing the amount that different classes depend on each other’s internal implementation, which is called coupling. Users of your class will be tempted to rely on details of your class’s implementation rather than on its logical interface. The good news is that the only easy way to change an object’s state in Ruby is by calling one of its methods. If you control access to the methods, you control access to the object. A good rule of thumb is never to expose methods that could leave an object in an invalid state.

Ruby gives you three levels ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.