Ruby-on-rails 以自用。与否.. 在 Rails 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/386115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:51:43  来源:igfitidea点击:

To use self. or not.. in Rails

ruby-on-railsruby

提问by Swamy g

I've been coding in Ruby for sometime now, but I don't understand when to use:

我已经用 Ruby 编写代码有一段时间了,但我不知道何时使用:

def self.METHOD_NAME
end

or just:

要不就:

def METHOD_NAME
end

In any Rails model. Is "self" a modifier like private in Java? When should I use it and when not?. Thanks a ton.

在任何 Rails 模型中。“self”是像Java中的private这样的修饰符吗?我什么时候应该使用它,什么时候不应该?万分感谢。

回答by DanSingerman

def self.method_name
end

defines a class method.

定义一个类方法。

def method_name
end

defines an instance method.

定义一个实例方法。

Thisis a pretty good post on it.

是一篇关于它的很好的帖子。

回答by krusty.ar

A quick explanation of what that means:

对这意味着什么的快速解释:

In ruby, you can define methods on a particular object:

在 ruby​​ 中,您可以在特定对象上定义方法:

a = "hello"

def a.informal
  "hi"
end

a.informal
=> "hi"

What happens when you do that is that the object a, which is of class String, gets its class changed to a "ghost" class, aka metaclass, singleton class or eigenclass. That new class superclass is String.

当你这样做时会发生什么是类的对象 aString将其类更改为“幽灵”类,即元类、单例类或特征类。那个新的超类是String.

Also, inside class definitions, selfis set to the class being defined, so

此外,在类定义中,self设置为正在定义的类,因此

class Greeting
  def self.say_hello
    "Hello"
  end
  #is the same as:
  def Greeting.informal
    "hi"
  end
end

What happens there is that the object Greeting, which is of class Class, gets a new metaclass with the new methods, so when you call

发生的事情是,属于Greetingclass的对象Class使用新方法获得了一个新元类,因此当您调用

Greeting.informal
=> "hi"

There's no such thing as class methods in ruby, but the semantics are similar.

ruby 中没有类方法这样的东西,但语义是相似的。

回答by bradheintz

A good guide on when to use which one:

关于何时使用哪个的好指南:

  • If the method depends on any internal state of the object, or must know which instance of the object it is addressing, then DO NOT make it a class (self.) method.
  • If the method does not depend on the state of the object, or on having a specific instance of the object, then in may be made a class method.
  • 如果该方法依赖于对象的任何内部状态,或者必须知道它正在寻址对象的哪个实例,则不要将其设为类 ( self.) 方法。
  • 如果该方法不依赖于对象的状态,或者不依赖于对象的特定实例,则 in 可以成为类方法。

When making a class method, think carefully about which class or module it belongs in. If you ever catch yourself duplicating code in class methods across classes, factor it into a module that other classes may mix in.

在创建类方法时,请仔细考虑它属于哪个类或模块。 如果您发现自己在跨类的类方法中复制代码,请将其分解为其他类可能混入的模块。

回答by Lolindrath

In this context - def self.method_name makes it sort of equivalent to the Java static method:

在这种情况下 - def self.method_name 使其相当于 Java 静态方法:

ruby:

红宝石:

class HexHelper
  def self.to_h(num)
    sprintf("%x", num)
  end
end

use: HexHelper.to_h(12345)

java:

爪哇:

public class HexHelper
{
  public static String toHex(int num)
  {
    return new PrintfFormat("%x").sprintf(num);
  }
}

use: HexHelper.toHex(12345)

回答by theschmitzer

self is alwaysthe current object

self始终是当前对象

When you see self here

当你在这里看到自己

def self.method_name end

def self.method_name end

You are not in an instance method, so self is the current Classobject.

您不在实例方法中,因此 self 是当前Class对象。

回答by Anurag

selfis like the thiskeyword in Java. It's a reference to the current object instance. If your model code performs an operation on the current object, then you'd probably need a function without self.method_name specifier.

self就像Java 中的this关键字。它是对当前对象实例的引用。如果您的模型代码对当前对象执行操作,那么您可能需要一个没有 self.method_name 说明符的函数。