ruby 相当于 c# 的内部关键字?
在 C# 中,“内部”类或方法只能从它所在的程序集中调用...我知道 Ruby 没有“程序集”或任何像它们一样的真实包(除了 gems,但它们不是)根本不是同一件事),但我想知道是否有一种方法可以限制可以调用方法的位置,类似于内部?
我正在更新一个小框架并创建一个包含 3 个方法的类。我希望能够仅从我的框架调用这 3 个方法中的 2 个,并从任何地方调用第三个方法。这在红宝石中可能吗?或者我是否会犯这个错误并且应该创建两个单独的类?或者 ?
in C# an "internal" class or method can only be called from within the assembly that it is located... i know Ruby doesn't have "assemblies" or any kind of real package like them (other than gems, but they aren't really the same thing at all), but I'm wondering if there is a way for me to limit the location that a method can be called from, similar to internal?
I'm updating a small framework and created a class that has 3 methods on it. I want to be able to call 2 of the 3 methods only from my framework, and the third method from anywhere. Is this possible in ruby? or am I going about this wrong and should be creating two separate classes? or ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,Ruby 是一种在封装方面相当缺乏的语言。它的容量非常有限,所以不,没有任何相当于 C# 内部关键字的东西。也许在您的情况下,两个单独的类会更好,但是清晰的文档字符串或特殊的命名方案可以在不进行大量修改的情况下执行相同的操作。
另外,您必须注意,internal 关键字只不过是为库开发人员提供方便。它并不能真正阻止某人使用标记为内部的方法或类,有办法通过代码注入、AOP 等来规避这些保护。
Unfortunately, Ruby is a language that's quite lacking in terms of encapsulation. It's capacity there are quite limited so, no, there isn't anything equivalent to C# internal keyword. Maybe in your case two separate classes would be better, but a clear docstring or a special naming scheme could do the same without heavy modifications.
Also, you must note that the internal keyword is nothing more than convenience for library developers. It doesn't truly prevent someone from using a method or class marked as internal, there's way to circumvent these protection with code injection, AOP, etc.