如何使用Ruby的self关键字

发布于 2024-11-19 20:15:43 字数 218 浏览 3 评论 0原文

根据我对 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 技术交流群。

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

发布评论

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

评论(4

心清如水 2024-11-26 20:15:43

有几个重要的用途,其中大部分基本上是为了消除实例方法、类方法和变量之间的歧义。

首先,这是定义类方法的最佳方式:

class Foo
  def self.bar
    "class method bar"
  end

  def bar
    "instance method bar"
  end
end

Foo.bar  #returns "class method bar"

foo = Foo.new
foo.bar #returns "instance method bar"

另外,在实例方法中 self 指的是实例,在类方法中它指的是类,并且它总是可以用来区分局部变量。

class Bar
  def self.foo
    "foo!"
  end

  def baz
    "baz!"
  end

  def self.success
    foo #looks for variable foo, doesn't find one, looks for class method foo, finds it, returns "foo!"
  end

  def self.fail
    baz #looks for variable baz, doesn't find one, looks for class method baz, doesn't find one, raises exception
  end

  def instance_success
    baz #looks for variable baz, doesn't find one, looks for instance method baz, finds it, returns "baz!"
  end

  def instance_fail
    foo #looks for variable foo, doesn't find one, looks for instance method foo, doesn't find one, raises exception
  end

  def local_variable
    baz = "is my favorite method"
    baz #looks for variable baz, finds it, returns "is my favorite method"
  end

  def disambiguate
    baz = " is my favorite method"
    self.baz + baz #looks for instance method baz, finds it, looks for local variable baz, finds it, returns "baz! is my favorite method"
  end
end

因此,最终,您可以在许多情况下避免使用 self,但使用它通常有助于确保以后不会无意中造成命名冲突。有时,这些可能会产生很难发现的错误。最终,这通常是个人风格的问题。


正如评论中指出的,还有一件非常重要的事情:

在一个类中,如果您有这样的方法:

def bar=(string)
  ...
end

并且在您调用的另一个方法中:

def other_method
  bar = "abcd"
end

它不会调用您的 bar= 方法,它将创建一个局部变量bar。因此,在本例中,您使用 self 告诉 Ruby 不要创建局部变量:

def other_method
  self.bar = "abcd"
end

如果您想采用方法名称作为参数,同样的情况也适用:

def example
  ...
end

def other_thing(example)
  self.example(example)
end

如果您省略了 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:

class Foo
  def self.bar
    "class method bar"
  end

  def bar
    "instance method bar"
  end
end

Foo.bar  #returns "class method bar"

foo = Foo.new
foo.bar #returns "instance method bar"

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.

class Bar
  def self.foo
    "foo!"
  end

  def baz
    "baz!"
  end

  def self.success
    foo #looks for variable foo, doesn't find one, looks for class method foo, finds it, returns "foo!"
  end

  def self.fail
    baz #looks for variable baz, doesn't find one, looks for class method baz, doesn't find one, raises exception
  end

  def instance_success
    baz #looks for variable baz, doesn't find one, looks for instance method baz, finds it, returns "baz!"
  end

  def instance_fail
    foo #looks for variable foo, doesn't find one, looks for instance method foo, doesn't find one, raises exception
  end

  def local_variable
    baz = "is my favorite method"
    baz #looks for variable baz, finds it, returns "is my favorite method"
  end

  def disambiguate
    baz = " is my favorite method"
    self.baz + baz #looks for instance method baz, finds it, looks for local variable baz, finds it, returns "baz! is my favorite method"
  end
end

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:

def bar=(string)
  ...
end

And in another method you call:

def other_method
  bar = "abcd"
end

It isn't going to call your bar= method, it's going to create a local variable bar. So, in this case you use self to tell Ruby not to create a local variable:

def other_method
  self.bar = "abcd"
end

The same thing applies if you want to take an argument with the name of a method:

def example
  ...
end

def other_thing(example)
  self.example(example)
end

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.

寄离 2024-11-26 20:15:43

在大多数情况下 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 write foo for the same effect, but in this case it is not and the self is required.

var_one = method(args) will create a local variable called var_one, it will not call any method or do anything else to self.

self.var_one = method(args) will call the method var_one= on self with the argument method(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 the self keyword.

落在眉间の轻吻 2024-11-26 20:15:43

self 的另一种用途是声明类方法(类似于 Java 中的静态方法)。

class foo
 def self.bar
  #do class related stuff here
 end
end

话虽这么说,您也可以使用 def foo.bar 来代替方法签名。

One other use of self is to declare class methods (similar to static methods in Java).

class foo
 def self.bar
  #do class related stuff here
 end
end

That being said, you could also have used def foo.bar instead for the method signature.

你是暖光i 2024-11-26 20:15:43

这是一个例子:

def run miles
  self.miles = miles
end

在这种情况下 self 会有所帮助。在大多数情况下,self 是多余的。

Here's an example:

def run miles
  self.miles = miles
end

In this case self will help. In most cases self is redundant.

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