(Ruby,Rails) 模块和库中的 SELF 上下文...?
关于在模块或库中使用“SELF”的快速问题。 基本上,“SELF”的范围/上下文是什么,因为它涉及模块或库,以及如何正确使用它? 有关我正在讨论的示例,请查看与“restful_authentication”一起安装的“AuthenticatedSystem”模块。
注意:我知道“self”在其他语言中等同于“this”以及“self”如何在类/对象上运行,但是在模块/库的上下文中没有什么“self”。 那么,在没有类的模块之类的东西中, self 的上下文是什么?
Quick question regarding the use of "SELF" inside a module or library. Basically what is the scope/context of "SELF" as it pertains to a module or library and how is it to be properly used? For an example of what I'm talking about, check out the "AuthenticatedSystem" module installed with "restful_authentication".
NOTE: I'm aware that 'self' equates to 'this' in other languages and how 'self' operates on a class/object, however in the context of a module/library there is nothing to 'self'. So then what is the context of self inside something like a module where there is no class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在模块中:
当您在实例方法中看到
self
时,它指的是包含该模块的类的实例。当您在实例方法之外看到
self
时,它指的是模块。In a module:
When you see
self
in an instance method, it refers to the instance of the class in which the module is included.When you see
self
outside of an instance method, it refers to the module.简短的总结...
http://paulbarry.com/articles/2008/ 04/17/the-rules-of-ruby-self
self 还用于添加类方法(或 C#/Java 人员的静态方法)。 以下代码片段将一个名为 do_something 的方法添加到当前类对象(静态)...
For a short summary...
http://paulbarry.com/articles/2008/04/17/the-rules-of-ruby-self
self is also used to add class methods (or static methods for C#/Java people). The following snippet is adding a method called do_something to the current class object (static)...