何时在 Ruby 方法中使用 `self.foo` 而不是 `foo`

发布于 2024-10-12 01:09:14 字数 330 浏览 8 评论 0原文

这不是 Rails 特有的 - 我只是使用 Rails 作为示例。

我在 Rails 中有一个模型:(

class Item < ActiveRecord::Base

  def hello
    puts "Hello, #{self.name}"
  end
end

假设 Item 模型(类)有一个名为 name 的方法。)什么时候需要使用 self.name 什么时候可以只使用name(例如,#{name})?

This is not specific for Rails - I am just using Rails as an example.

I have a model in Rails:

class Item < ActiveRecord::Base

  def hello
    puts "Hello, #{self.name}"
  end
end

(Let's assume that the Item model (class) has a method called name.) When do I need to use self.name and when can I just use name (e.g., #{name})?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

少跟Wǒ拽 2024-10-19 01:09:15
  1. 调用方法时更喜欢省略self.是惯用的做法;通常不需要它。

  2. 调用 setter 方法时必须使用 self.foo = xxx,而不是 foo = xxx ,以便 Ruby 意识到您并不是在尝试创建新的局部变量。

    • 同样,万一您有一个与方法同名的现有局部变量 do_something,则必须使用 self.do_something 来调用方法,因为 do_something 最终会读取变量。
  3. 不能使用 self.foo(.. .) 调用私有方法;您必须只调用 foo(...)

  1. It is idiomatic to prefer to omit self. when invoking methods; it is generally never needed.

  2. You must use self.foo = xxx when calling a setter method, instead of foo = xxx, so that Ruby realizes that you are not trying create a new local variable.

    • Similarly, in the unlikely event that you have an existing local variable do_something with the same name as a method, you must use self.do_something to invoke the method, as just do_something will end up reading the variable.
  3. You cannot use self.foo(...) to call a private method; you must instead call just foo(...).

誰認得朕 2024-10-19 01:09:15

如果省略 self Ruby 将首先查找具有该名称的局部变量,然后查找实例方法。写成self. 并不符合习惯用法。无论如何,您都必须在赋值时编写 self.something = value

请注意,调用私有方法时不能使用 self(受保护的方法没有问题):

class A
  def foo; self.bar; end

private

  def bar; "bar"; end
end

A.new.foo  
# private method `bar' called for #<A:0x7f49193584f0> (NoMethodError)

If you omit self Ruby will first look for local variables with that name, then for an instance method. It's not idiomatic to write self.. In any case, you have to write self.something = value on assignations.

Note that you cannot use self when calling private methods (no problem with protected methods):

class A
  def foo; self.bar; end

private

  def bar; "bar"; end
end

A.new.foo  
# private method `bar' called for #<A:0x7f49193584f0> (NoMethodError)
栩栩如生 2024-10-19 01:09:15

遵循本教程,您不需要使用self指针。但我认为 this (或在我们的例子中 self)指针用于解决名称冲突。实际上,@nameself.name 是相同的语句(如果您的类没有 name 方法)。例如:

class Moo
  attr_accessor :name

  def moo(name)
    name = name # O_o which *name* should i use?
  end

  def foo(name)
    @name = name # the same as *self.name = name*
  end

  def hello
    puts self.name # the same as *puts @name*
  end
end

a = Moo.new
a.hello() # should give no output

a.moo('zaooza')
a.hello() # Hey! Why does it prints nothing?

a.foo('zaooza')
a.hello() # whoa! This one shows 'zaooza'!

尝试运行此代码,您会看到 =)

Following this tutorial, you have no need to use self pointer. But i think this (or self in our case) pointers are used to resolve name conflicts. Actually, @name and self.name are the same statements (if there is no name method for your class). E.g.:

class Moo
  attr_accessor :name

  def moo(name)
    name = name # O_o which *name* should i use?
  end

  def foo(name)
    @name = name # the same as *self.name = name*
  end

  def hello
    puts self.name # the same as *puts @name*
  end
end

a = Moo.new
a.hello() # should give no output

a.moo('zaooza')
a.hello() # Hey! Why does it prints nothing?

a.foo('zaooza')
a.hello() # whoa! This one shows 'zaooza'!

Try running this code and you'll see =)

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