January 2024
Intermediate to advanced
718 pages
20h 15m
English
Method definition and class inheritance have a wrinkle, but it’s fairly obscure. Within a class definition, you can change the visibility of a method in an ancestor class. For example, you can do something like this:
| | class Base |
| | def a_method |
| | puts "Got here" |
| | end |
| | private :a_method |
| | end |
| | |
| | class MakeItPublic < Base |
| | public :a_method |
| | end |
| | |
| | class KeepItPrivate < Base |
| | end |
In this example, you could invoke a_method in instances of class MakeItPublic, but not via instances of Base or KeepItPrivate.
So, how does Ruby pull off this feat of having one method with two different visibilities? Simply put, it cheats.
If a subclass changes the visibility of a method in a parent, Ruby effectively ...
Read now
Unlock full access