What is method visibility in Ruby?
Ruby’s concept of method visibility determines a method’s accessibility or whether other classes are permitted to utilize a given field or execute a certain method in a given class.
There are three levels of method visibility in ruby. They are as follows:
- Private methods
- Public methods
- Protected methods
Private methods
Only other instance methods of the class may invoke a private method, which is intrinsic to the implementation of the class (subclass). We cannot use private methods explicitly. Instead, they implicitly invoke themselves.
Public methods
Methods are typically public unless they are specifically designated as private or protected. Since the initialize function is always implicitly private, it is an exception. A public method has no limitations on its use and can be invoked from anywhere.
Protected methods
Similar to the private method, we can only use a protected method within the implementation of its class or subclasses. In contrast to private methods—which we can only use implicitly on the object itself—protected methods can directly call on any class instance.
Free Resources