简体   繁体   English

在Rails的一个控制器中使用私有和受保护的方法

[英]Using private and protected methods in one controller in Rails

All the articles say about the difference between private and protected methods, however there's no clearance about using it. 所有文章都谈到了私有方法和受保护方法之间的区别,但是使用它并没有明确的依据。

So if code something like: 因此,如果代码类似:

private
  def my_method
    #some code
  end

Does private affect only the my_method or everything below? 私有只会影响my_method或下面的所有内容吗?

UPDATE: And if affects everything what if I want to use protected methods as well? 更新:如果影响到一切,那么如果我也想使用受保护的方法呢? If I code below my_method: 如果我在my_method下面编码:

protected
  def another_method
    #some code
  end

Does it mean that private method has ended and protected methods section has started? 这是否意味着私有方法已经结束而保护方法部分已经开始?

To simply answer your question: yes, when you have the following code: 简单回答您的问题:是,当您具有以下代码时:

private
  ....

protected
  ....

Then private stops where protected begins. 然后在受保护的地方开始私人站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM