Should you use private, protected, and public modifiers in Ruby?

I get more from C # background, but learn Ruby in my free time.

In the given classes, you can create your own methods private , public (by default) or protected . As far as I understand their use, it is typical for Ruby code to use such modifiers, despite the dynamic language in which the user can easily redefine access?

When using something like Send user can access the private methods independently, I'm just wondering what works best for Ruby and access modifiers? In other words, should they be used in their classes?

+4
source share
3 answers

In the given classes, you can make your methods private, public (by default) or protected. As far as I understand their use, it is typical for Ruby code to use such modifiers, despite the dynamic language in which the user can easily redefine access?

It is still relatively common, as it conveys intent and minimizes the interface you are viewing. Some conventions:

  • Just omit public , as all methods are public unless otherwise specified, and methods are usually grouped using access modification.

  • Despite the fact that the user can redefine access, this does not mean that they should (almost the same way, you don’t see that C # developers do crazy things, for example, inject IL stubs into classes so that they can access private methods). Therefore, it is still useful to have a distinction.

  • Protected methods are somewhat less common than they were in C # because Ruby really does not encourage inheritance as a means of conveying behavior. General behavior can often be reorganized into Module , and then include/extend -ed if necessary.

  • Private methods are about as common as in C #; Ruby often prefers a lot of tiny methods that do very specific things that you then put together to get something useful. But you do not disclose these methods, because they are not part of your open interface, and you want to keep this mess.

  • Because private methods are not considered part of the interface, you should be free to change them with impunity. By making them private, you have added to other users that these methods and their implementation or definitions can change at any time.

+8
source

I always thought it would be exposing the interface. You do not want your users to be overloaded with your implementation, so you make such things as private. He is kinder to them. For example, when dealing with arrays, you really want 71 more methods in your path (which you almost certainly don't care about?)


 RUBY_VERSION # => "1.8.7" Array.new.public_methods.size # => 149 Array.new.private_methods.size # => 71 

Another important part of private methods is “subject to change”. When you are committed to the interface, you must support it. You cannot break every code every time a new version is downloaded. Things that are private are understood to be volatile. They are not affected, so it is understood that you can change them. If users created code based on private methods, then this is not your mistake (if they do not do this b / c, you gave them the crap interface). Therefore, you can make it private in order to give yourself the opportunity to do it better in the future.

+4
source

I would still use them in your code, as it conveys a clear intention to the code reader, and also gives you the necessary protection. If another developer overrides your methods or calls them directly through #send, they most likely either have or have good reason, or at least understand why they should do this.

Using Private / Protected in your code makes things more obvious to the future developer who will override your methods, and also works correctly for you if no one overrides them.

+2
source

Source: https://habr.com/ru/post/1313953/


All Articles