A
A
Alexander Grishin2015-10-14 13:45:37
Ruby on Rails
Alexander Grishin, 2015-10-14 13:45:37

Why are there public, protected and private methods in ruby?

I found the answer to my question here:
answer
But there is a question to one of the answers. And specifically what I can't understand: Victor Ablebeam 's
answer : Protected are those methods that are available to descendants (classes that inherit), but not available to external classes. Roughly speaking, it is convenient when you know that many, many different classes will inherit from class A, and these classes will have one (or many, it doesn’t matter) common method. Which, however, is completely unnecessary for external classes. If there were no protected, there would be 2 ways - to make a method that all descendants should have - public. This is bad because it is redundant information for external classes. Or - duplicate the same methods in each descendant class. It's bad because it's not DRY.

My question is:
If it is inherited, then who will know that it even exists? Because the person who found this class was probably looking for it according to the final method that he needs. Yes, even if the one who needs some public method of this class finds out about the existence of a protected method of the found class, he will probably not use it. Hence the conclusion - why not just use the public method that is not documented by the capabilities of the class or is simply indicated in the documentation as auxiliary and, accordingly, do not touch it, thereby saving a line of code in the form of a method type declaration and eliminating confusion on the Internet associated with these common questions about defining the type of methods. Is not it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DevMan, 2015-10-14
@beerdy

we save a line of code in the form of a method type declaration
victory wins, congratulations.
and eliminate the confusion on the Internet associated with these common questions about the definition of the type of methods
documentation clears confusion. and if it is not there, then the public method or the private / protected one does not play any piano.

A
Alexey Ukolov, 2015-10-14
@alexey-m-ukolov

If it is inherited, then who will know that it even exists?
Someone who uses a decent IDE.
Yes, even if the one who needs some public method of this class finds out about the existence of a protected method of the found class, he will probably not use it.
Absolutely not - this is a protected method, it cannot be called just like that.
Well, everything that is written further is completely incomprehensible to me - some undocumented features, lines of code ...

A
Alexander Grishin, 2015-10-14
@beerdy

Here again I read the branch that I indicated above and found the answer of the same author Victor Ablebeam sounds like a trace. way:
I would say that in ruby, by definition, there cannot be a normal scope, because everything is defined in runtime. As a result, all these notations are more documentary in nature. And, at least once, using protected in controllers and helpers was considered at least good form

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question