您在 Rails 中使用过受保护的可见性吗?

发布于 2024-07-21 13:37:43 字数 177 浏览 8 评论 0原文

坦白:我的方法只使用私有公共可见性!

我有一种感觉,这是一件坏事。 但在 Rails 中,这似乎并不是一个问题。

有没有人在Rails中有一个例子,其中不使用受保护的可见性将是一个很大的错误?

Confession: I only use private and public visibility for my methods!

I have a feeling this is a bad thing. But in Rails it just doesn't seem to come up as an issue.

Does anyone have an example in Rails where it would be a big mistake not to use protected visibility?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-07-28 13:37:43

更新 - 请参阅下面链接到 Ruby 中 protected/private 的真实解释。 这确实是我在爪哇时代留下的根深蒂固的偏见。 我的答案中唯一重要的部分是,不是操作的控制器方法不应该是公共的(或者至少你的路由应该保护它们)。

单表继承是 protected 在模型层中发挥作用的完美示例,因为它是继承最常见的用途之一。

在控制器层中,在 ApplicationController 上定义的辅助方法应标记为受保护——如果它们是私有,其他控制器将无法访问它们,但如果它们是公共的,Rails 会将它们视为操作。

就我个人而言,我发现我比许多朋友和同事更多地使用类继承,即使在 Rails 应用程序中也是如此。 因为我经常使用它(并且是在我的 Java 时代),所以我喜欢对所有辅助方法使用 protected ,以便为想要扩展该类的任何人(通常是我自己)提供自由 —— 除非我真的很尴尬,然后我将其标记为私人。 :)

Update -- Please see the comment below that links to a true explanation of protected/private in Ruby. That was a deep seated prejudice left over from my Java days, indeed. The only important part left to my answer is that controller methods that are not actions should not be public (or at least your routes should protect them).

Single Table Inheritance is a perfect example of when protected is helpful in the model tier, as it's one of the most common uses of inheritance there.

In the controller tier, helper methods defined on ApplicationController should be marked as protected -- if they were private the other controllers would not be able to access them, but if they are public Rails will treat them as actions.

Personally, I find that I use class inheritance more than many of my friends and coworkers, even in Rails applications. Because I use it often (and coming out of my Java days), I favor protected for all helper methods to give freedom to anyone (usually myself) who wants to extend the class -- unless I'm really really embarrassed about one, then I mark it private. :)

爱已欠费 2024-07-28 13:37:43

我有 SingleTableInheritance

类 Person < AR::基础
班主任< 人
班级学生< 我

使用受保护的方法来实现学生和教师常见的私有方法:

class Person < AR::base
  def self.find(*args)
    reject_leaves(super(*args))
  end
protected
  def self.reject_leaves(target) #like a private in Teacher and Student
    case target
      when Array target.select{|t| reject_leaves(t)}
      when Person (target.leave_date < Date.today ? target : nil)
      else target
    end
  end
end

免责声明:有像 act-as-paranoid 等插件来实现我在这里用来向您展示案例的功能,但我有更多复杂的景观,我在这里简化了以表达你的观点。

I have SingleTableInheritance

class Person < AR::base
class Teacher < Person
calss Student < Person

And I use the protected methods to implement a private method that is common for Student and Teacher:

class Person < AR::base
  def self.find(*args)
    reject_leaves(super(*args))
  end
protected
  def self.reject_leaves(target) #like a private in Teacher and Student
    case target
      when Array target.select{|t| reject_leaves(t)}
      when Person (target.leave_date < Date.today ? target : nil)
      else target
    end
  end
end

Disclaimer: There are plugins like act-as-paranoid and other to implement the feature I use here to show you the case but I have a more complex landscape, that I have simplified here to get to your point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文