'self.method_name' 和 'class <&lt; 之间的区别Ruby 中的“自我" [英] Difference between &#39;self.method_name&#39; and &#39;class &lt;&lt; self&#39; in Ruby

查看:41
本文介绍了'self.method_name' 和 'class <&lt; 之间的区别Ruby 中的“自我"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个类的实例化限制为一个(不使用单例),但我不能.我尝试使用类变量(@@)但没有运气.我用谷歌搜索了一下,发现了这个:

I was trying to limit the instantiation of a class to just a single one(without using singleton) but i couldn't. I tried with class variables (@@) but without luck. I googled it and came across this:

class A 
  @count = 0 

  class << self 
    attr_accessor :count 
  end

  def initialize val 
    @a = val 
    self.class.count += 1 
  end 
end 

a=A.new 42 
b=A.new 43

我搜索了 'class <<self' 解释希望找到一个更好的(或者只是一个更简单和干净的),但又没有运气.最后,经过一些测试,我得出结论 'class <<self' 只是一个块包装器,您可以在其中定义 class 方法.那么,这是正确的吗?

I searched for the 'class << self' explanation hoping to find a better(or just a more simple and clean) but againt, no luck. Finally, after some tests i concluded that 'class << self' is just a block wrapper where you can define class methods. So, is this the correct?

问候!

推荐答案

class <<self 符号打开了一个对象的特征类.特征类是存储特定于实例的行为的匿名类.在类的情况下,本征类有时称为元类.

The class << self notation opens up the eigenclass of an object. An eigenclass is an anonymous class that stores instance-specific behaviour. In the case of a class an eigenclass is sometimes called a metaclass.

Ruby 使用特征类来实现所谓的类方法"(也称为静态方法).

Ruby uses eigenclasses to implement so called 'class methods' (also called static methods).

类(如 moritz 所述)在 Ruby 中也是一个对象,只要它是一个对象,它也有一个类.Ruby 中类的类称为 Class.

A Class (as moritz stated) is also an Object in Ruby and in so far as it is an object it also has a class. The class of a class in Ruby is called Class.

任何语言中的类方法"都是以类为接收方的方法——即直接在类本身上调用该方法.

A 'class method' in any language is a method in which a class is the receiver - that is the method is directly invoked on the class itself.

但是,为了在接收器上调用方法,必须在该接收器的类上定义该方法.对于类,类方法"可以作为 Class 类的实例方法实现.

However in order for a method to be invoked on a receiver that method must be defined on the class of that receiver. In the case of classes a 'class method' could be implemented as an instance method on the Class class.

但是在 Class 上定义实例方法意味着所有类都可以访问该类方法,这并不理想.

But defining an instance method on Class would mean that ALL classes get access to that class method which is not ideal.

进入特征类,如前所述,对象的特征类是一个特殊的类,用于存储该对象独有的方法.对于类,本征类是 Class 类的子类,并且是该类的直接类.

Enter the eigenclass, as stated before, the eigenclass for an object is a special class that stores the methods unique to that object. In the case of classes the eigenclass subclasses the Class class and is the direct class of the class.

因此,Ruby 中的类方法"只是在类的特征类上定义的实例方法".

'class methods' in Ruby therefore are just 'instance methods' defined on the class's eigenclass.

def MyClass.my_method 表示法实际上在 MyClass 的特征类上定义了 my_method.如果您使用这种表示法,您可以在不真正理解特征类的情况下(暂时)通过,因为您可以欺骗自己认为这只是 Ruby 定义静态方法"的方式,并继续认为 Ruby 的类模型类似于 Java 的类模型.但是,class << 符号不允许这样的解释,您必须接受特征类的现实.

The def MyClass.my_method notation actually defines my_method on the eigenclass of MyClass. If you use this notation you can get by (for a while) without actually understanding eigenclasses since you can trick yourself into thinking it is just Ruby's way of defining 'static methods' and continue thinking Ruby's class model is similar to Java's. However, the class << self notation allows no such interpretation and you must come to terms with the reality of eigenclasses.

总而言之,类方法"实际上是在特征类和类<<<;上定义的实例方法".self 使您可以访问特征类.

In summary, 'class methods' are actually 'instance methods' defined on the eigenclass and the class << self gives you access to the eigenclass.

有关更多阅读,请查看以下链接:

For more reading check out these links:

http://banisterfiend.wordpress.com/2008/11/25/a-complete-ruby-class-diagram/

http://banisterfiend.wordpress.com/2008/10/25/the-secret-life-of-singletons/

http://www.klankboomklang.com/2007/09/21/the-singleton-class/

这篇关于'self.method_name' 和 'class <&lt; 之间的区别Ruby 中的“自我"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆