ruby def `self.function` 名称是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13706373/
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-06 05:34:23  来源:igfitidea点击:

What does def `self.function` name mean?

ruby

提问by Dude

Can anyone explain to me what the meaning of adding selfto the method definition is? Is it similar to the thiskeyword in java?

谁能向我解释添加self到方法定义中的含义是什么?是不是类似于thisjava中的关键字?

回答by BernardK

Contrary to other languages, Ruby has no class methods, but it has singleton methods attached to a particular object.

与其他语言相反,Ruby 没有类方法,但它具有附加到特定对象的单例方法。

cat = String.new("cat")
def cat.speak
    'miaow'
end
cat.speak #=> "miaow" 
cat.singleton_methods #=> ["speak"] 

def cat.speakcreates a singleton method attached to the object cat.

def cat.speak创建一个附加到对象 cat 的单例方法。

When you write class A, it is equivalent to A = Class.new:

当你写时class A,它相当于A = Class.new

A = Class.new
def A.speak
    "I'm class A"
end
A.speak #=> "I'm class A" 
A.singleton_methods #=> ["speak"] 

def A.speakcreates a singleton method attached to the object A. We call it a class method of class A.

def A.speak创建一个附加到对象 A 的单例方法。我们称它为类 A 的类方法。

When you write

当你写

class A
    def self.c_method
        'in A#c_method'
    end
end

you create an instance of Class(*). Inside the class definition, Ruby sets self to this new instance of Class, which has been assigned to the constant A. Thus def self.c_methodis equivalent to def cat.speak, that is to say you define a singleton method attached to the object self, which is currently the class A.

您创建Class(*)的实例。在类定义中,Ruby 将 self 设置为 Class 的这个新实例,该实例已分配给常量 A。因此def self.c_method等价于def cat.speak,也就是说您定义了一个附加到对象 self 的单例方法,它当前是类 A .

Now the class A has two singleton methods, that we commonly call class methods.

现在类 A 有两个单例方法,我们通常称之为类方法。

A.singleton_methods
 => ["c_method", "speak"] 

(*) technically, in this case where Ahas already been created by A = Class.new, class Areopens the existing class. That's why we have two singleton methods at the end. But in the usual case where it is the first definition of a class, it means Class.new.

(*) 从技术上讲,在这种情况下 whereA已经被创建A = Class.newclass A重新打开现有的类。这就是为什么我们最后有两个单例方法。但在通常情况下,它是类的第一个定义,这意味着Class.new.

回答by krichard

In ruby self is somewhat similar to thisin java, but when it comes to classes its more like the keyword staticin java. A short example:

在 ruby​​ 中 self 与this在 java 中有些相似,但是当涉及到类时,它更像是staticjava 中的关键字。一个简短的例子:

class A 
  # class method 
  def self.c_method
    true
  end
  # instance method
  def i_method
    true
  end
end

A.c_method #=> true
A.i_method #=> failure
A.new.i_method #=> true
A.new.c_method #=> failure

Update:Difference between static methods in java and class methods in ruby

更新:java 中的静态方法和 ruby​​ 中的类方法之间的区别

Static methods in Java have two distinct features that makes them different from instance methods: a) they are static, b) they are not associated with an instance. (IOW: they really aren't like methods at all, they are just procedures.) In Ruby, all methods are dynamic, and all methods are associated with an instance. In fact, unlike Java where there are three different kinds of "methods" (instance methods, static methods and constructors), there is only one kind of method in Ruby: instance methods. So, no: static methods in Java are completely unlike methods in Ruby. –?J?rg W Mittag 1 hour ago

Java 中的静态方法有两个不同的特性,使它们与实例方法不同:a)它们是静态的,b)它们不与实例相关联。(IOW:它们真的根本不像方法,它们只是过程。)在 Ruby 中,所有方法都是动态的,并且所有方法都与一个实例相关联。实际上,与Java 中存在三种不同的“方法”(实例方法、静态方法和构造函数)不同,Ruby 中只有一种方法:实例方法。所以,不:Java 中的静态方法与 Ruby 中的方法完全不同。–?J?rg W Mittag 1 小时前

回答by kostja

When declaring a method, the selfof the declaration is the declaring class/module, so effectively you are defining a class method. For the client, this works similar to a staticmethod in java. The client would call the method on the class instead of an instance: MyClass.method

在声明一个方法时,self声明的对象是声明类/模块,因此您实际上是在定义一个类方法。对于客户端,这类似于staticjava 中的方法。客户端将调用类上的方法而不是实例:MyClass.method

Hereyou can find some more details on class and instance methods.

在这里您可以找到有关类和实例方法的更多详细信息。

EDIT: While the selfkeyword is akin to the thiskeyword in java, the effects of using selffor class method declaration are similar to the effect of using the statickeyword in java. The similarity is that static methods in java, like class methods in ruby are accessed using the class object iself instead of an instance of the class.

编辑:虽然self关键字类似于thisjava 中的关键字,但self用于类方法声明的效果类似于static在 java中使用关键字的效果。相似之处在于 java 中的静态方法,如 ruby​​ 中的类方法,都是使用类对象本身而不是类的实例来访问的。

Please note that staticdoes not stand for the opposite of dynamic. The choice of the name for this keyword is questionable (probably inherited from C) and rather should have been called perClassor similar to better reflect the meaning. The technical meaning is that all staticmembers exist only once for each classloader.

请注意,static这并不代表动态的对立面。这个关键字的名称选择是有问题的(可能是从 C 继承的),而应该被称为perClass或类似以更好地反映含义。技术含义是static每个类加载器的所有成员只存在一次。