如何使用Ruby的self关键字
根据我对 self 的理解,它指的是该类的当前实例。
无论如何,这不是始终的默认行为吗?例如, 不
self.var_one = method(args)
等于
var_one = method(args)
如果是的话, self
有什么用?
From what I understand about self
, it refers to the current instance of the class.
Isn't this the default behaviour at all times anyways? For example, isn't
self.var_one = method(args)
equivalent to
var_one = method(args)
If so, what is the use of self
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有几个重要的用途,其中大部分基本上是为了消除实例方法、类方法和变量之间的歧义。
首先,这是定义类方法的最佳方式:
另外,在实例方法中
self
指的是实例,在类方法中它指的是类,并且它总是可以用来区分局部变量。因此,最终,您可以在许多情况下避免使用
self
,但使用它通常有助于确保以后不会无意中造成命名冲突。有时,这些可能会产生很难发现的错误。最终,这通常是个人风格的问题。正如评论中指出的,还有一件非常重要的事情:
在一个类中,如果您有这样的方法:
并且在您调用的另一个方法中:
它不会调用您的
bar=
方法,它将创建一个局部变量bar
。因此,在本例中,您使用self
告诉 Ruby 不要创建局部变量:如果您想采用方法名称作为参数,同样的情况也适用:
如果您省略了
self
Ruby 会假设您指的是同名的局部变量。因此,一般来说,方法名称中的 self 用于区分类变量和实例变量,当 Ruby 需要帮助区分方法调用和局部变量或局部变量赋值时,在其他任何地方都可以使用它。
我希望这是有道理的。
There are several important uses, most of which are basically to disambiguate between instance methods, class methods, and variables.
First, this is the best way to define class methods:
Also, within instance methods
self
refers to the instance, within class methods it refers to the class, and it can always be used to distinguish from local variables.So, in the end, you can avoid using
self
in many cases, but it's often helpful to use it to make sure that you don't inadvertently create naming conflicts later on. Sometimes those can create bugs that are very hard to find. In the end it's often a matter of personal style.As noted in the comments, one more really important thing:
In a class, if you have a method like this:
And in another method you call:
It isn't going to call your
bar=
method, it's going to create a local variablebar
. So, in this case you useself
to tell Ruby not to create a local variable:The same thing applies if you want to take an argument with the name of a method:
If you left off
self
Ruby would assume you meant the local variable with the same name.So, in general,
self
in method names is used to distinguish between class and instance variables, and everywhere else you use it when Ruby needs help distinguishing between method calls and local variables or local variable assignment.I hope that makes sense.
在大多数情况下
self.foo
确实是多余的,因为您只需编写foo
即可获得相同的效果,但在这种情况下,它不是多余的,而self
> 是必需的。var_one = method(args)
将创建一个名为var_one
的局部变量,它不会调用任何方法或对self
执行任何其他操作。self.var_one = method(args)
将使用参数method(args)
调用self
上的方法var_one=
>。另一种使用
self
是非可选的情况是,如果您想将其作为参数传递给方法,即some_method(self)
- 您不能无需使用self
关键字即可完成此操作。In most cases
self.foo
is indeed redundant because you can just writefoo
for the same effect, but in this case it is not and theself
is required.var_one = method(args)
will create a local variable calledvar_one
, it will not call any method or do anything else toself
.self.var_one = method(args)
will call the methodvar_one=
onself
with the argumentmethod(args)
.Another case where the use of
self
is non-optional would be if you want to pass it as an argument to a method, i.e.some_method(self)
- you can't do that without theself
keyword.self 的另一种用途是声明类方法(类似于 Java 中的静态方法)。
话虽这么说,您也可以使用 def foo.bar 来代替方法签名。
One other use of
self
is to declare class methods (similar to static methods in Java).That being said, you could also have used
def foo.bar
instead for the method signature.这是一个例子:
在这种情况下
self
会有所帮助。在大多数情况下,self
是多余的。Here's an example:
In this case
self
will help. In most casesself
is redundant.