Ruby 中的私有/受保护块?
Ruby 似乎没有像这样定义受保护/私有块的功能:
protected do
def method
end
end
相比,这会很好
protected
def method
end
public
与您可能忘记在受保护方法之后“公开”的情况
。 似乎可以使用元编程来实现这一点。 有什么想法吗?
Ruby doesn't seem to have a facility for defining a protected/private block like so:
protected do
def method
end
end
This would be nice compared to
protected
def method
end
public
where you might forget to "public" after the protected methods.
It seems possible to implement this using metaprogramming. Any ideas how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您想按功能进行分组,因此您可以声明所有方法,然后通过使用 protected 后跟要保护的方法的符号来声明哪些方法是受保护的和私有的,对于私有方法也是如此。
下面的课程展示了我的意思。 在这个类中,除了 bar_protected 和 bar_private 之外,所有方法都是公共的,它们最后声明为 protected 和 private。
Since you want to group by functionality you can declare all your methods, and then declare which ones are protected and private by using protected followed by the symbols of the methods you want to be protected, and the same for private.
The following class shows what I mean. In this class all methods are public except bar_protected and bar_private which are declared protected and private at the end.
我实际上赞同 bodnarbm 的解决方案,并且不建议这样做,但由于我不能放弃元编程挑战,因此这里有一个可以完成此任务的 hack:
I actually endorse bodnarbm's solution and do not recommend doing this, but since I can't pass up a metaprogramming challenge, here's a hack that will accomplish this:
老问题,但我认为这更干净一些:
Old question, but I think this is a bit cleaner: