Method Visibility

There are three types of method visibility:

Public

Callable from anywhere

Protected

Callable only from instances of the same class

Private

Callable only in functional form (i.e., without the receiver specified)

Method visibility is defined using the public, private, and protected methods in classes and modules.

public([symbol...])

Makes the method specified by symbol public. The method must have been previously defined. If no arguments are specified, the visibility of all subsequently defined methods in the class or module is made public.

protected([symbol...])

Makes the method specified by symbol protected. The method must have been previously defined. If no arguments are specified, the visibility of all subsequently defined methods in the class or module is made protected.

private([symbol...])

Makes the method specified by symbol private. The method must have been previously defined. If no arguments are specified, the visibility of all subsequently defined methods in the class or module is made private.

Get Ruby in a Nutshell 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.