Don't "Use" Protected Methods in Ruby
I’ve just published an article on protected methods in Ruby. In this article, I discuss a proper usage of protected methods and try to shed some light on when they’re appropriate via an easy-to-understand example.
Comments
Even though the last_name is a method, it is still data. The first implementation of the Classroom class is reaching for the data in some other class instead of asking that class to perform a service. This is an example for wrong allocation of responsibilities. Ask for service to be performed, don’t ask for data and manipulate on that data to provide a service. The last version of Student encapsulates the last_name method and provides a higher level abstraction by implementing the spaceship operator that operates on the data.
Post a comment