B
B
basania2015-07-29 00:05:25
Ruby on Rails
basania, 2015-07-29 00:05:25

Please explain how self and protected work in Ruby?

Hello, I am a beginner programmer, I have a question with self and protected. I can’t understand how protected differs from private, I looked at the English-language forums, but I still didn’t understand anything, I would like a simple example. I saw a similar question related to Python, but unfortunately I don't know it.
I also can't figure out how self works. I noticed that it is sometimes used in methods.
To be more specific, I saw the code on callbacks and I don’t understand why there is protected here in the self method.

class User < ActiveRecord::Base
  validates :login, :email, presence: true
  before_validation :ensure_login_has_a_value
  protected
    def ensure_login_has_a_value
      if login.nil?
        self.login = email unless email.blank?
      end
    end
end

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2015-07-29
@basania

Public - should be used very carefully. Less is better. This is an external API for other classes that will use it. And the less you can learn, the faster you can learn and remember it, the fewer mistakes you can make.
protectedare those methods that are available to descendants (classes that inherit), but are 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.
Privateare those methods that are available only to the class. That is, roughly speaking, you can use them only in the same file where the class definition is. For example, it is convenient to use them for refactoring. Imagine that there is a public method that has been used by 100 different, unknown classes for many years. But in this method - 500 lines. You cannot change the method, so you turn it into 10 private method calls.
The best way to learn this is by creating different variations and trying different methods. 2 important things to remember in Ruby:
1) Different keywords for class and instance methods (private, private_class_method)
2) If you are testing behavior in the console using #send, the behavior will be non-OOP - #send ignores method visibility
Everything is easier with self. By default, self is always substituted. The method is called first for the current object - then it looks for the presence of methods in the ancestor, etc. etc. to the Object. Why sometimes explicitly write self ? Because in Ruby you can create view methods def method=() end;and use them with a space: method = 2. To do this, in order to make it clear that it is the method that is used, and not a local variable is defined, sometimes they resort to the full notation.
PS If something is really not clear even here - take any book on OOP or Wikipedia and try everything in the console in any programming language until you get the point.

D
D', 2015-07-29
@Denormalization

Oh my friend. You need to learn the basics of OOP. All sorts of encapsulations, abstractions, polymorphisms.
If you are explained here, it will not help you. You don't know the basics .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question