“<<<类别”的目的是什么?类” (尖括号)语法? [英] What is the purpose of the "class << Class" (angle brackets) syntax?

查看:107
本文介绍了“<<<类别”的目的是什么?类” (尖括号)语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为什么要使用 class<<向类中添加任何内容?类语法?

class Fun
    def much_fun
        # some code here
    end
end

class << Fun # the difference is here!
    def much_more_fun
        # some code here
    end
end

而不是使用猴子补丁/鸭子打孔方法:

instead of using the monkey patching/duck punching method:

class Fun
    def much_fun
        # some code here
    end
end

class Fun # the difference is here!
    def much_more_fun
        # some code here
    end
end






在阅读为什么要对Ruby 我碰到过:

为什么定义一个类 LotteryDraw

class LotteryDraw
    # some code here
    def LotteryDraw.buy( customer, *tickets )
        # some code here as well
    end
end

不久后,在 LotteryDraw 类:

class << LotteryDraw
    def play
        # some code here
    end
end

说:


当您看到 class<< obj ,请相信您的内心,我直接将 obj 的定义添加进去。

When you see class << obj, believe in your heart, I’m adding directly to the definition of obj.

此语法的目的是什么?为什么为什么决定以这种方式而不是使用猴子修补方法呢?

What is the purpose of this syntax? Why did Why decide to do it this way instead of using the monkey patching method?

这些是一些相关的问题和网站:

These are some related questions and websites:

  • Adding a new method to the Array class
  • Yield in class << self in class method, which gives me a little bit of an idea why I would like to use class << Class syntax.
  • Open Classes in Ruby

推荐答案

还需要更多解释。在ruby中,几乎每个对象都可以创建一个奇怪的东西,称为实例类。这个东西就像一个普通的类,主要区别是它只有一个实例,并且该实例是在该类之前创建的。

A little more explanation is needed. In ruby almost every single object can create a weird thing called an instance class. This thing is just like a normal class, the main difference is that it has only one single instance and that that instance is created before that class.

简而言之,拥有类A,您可以这样做:

In short, having class A, you can do:

a = A.new
b = A.new

a b 现在是类A的实例,并且可以访问在该类中定义的所有实例方法。现在,我们要添加一个额外的方法,该方法只能由 a 进行访问,而不能由 b 进行访问(在某些情况下这可能很有用,但请尽可能避免使用)。所有方法都在类中定义,因此似乎我们需要将其添加到类A中,但是通过 b 也可以访问此方法。在这种情况下,我们需要为 a 对象创建一个实例类。为此,我们使用 class<< < object> 语法:

Both, a and b are now the instances of class A and have an access to all the instance method defined within this class. Now let's say, we want to add an extra method which can only be accesses by a, but not by b (this might be useful in some cases, but rather avoid it if possible). All the methods are defined within the classes, so it seems we need to add it to class A, but this way it will be accessible by b as well. In this case we need to create an instance class for a object. To do that, we use class << <object> syntax:

class << a
  def foo
  end
end

a.foo #=> nil
b.foo #=> undefined method

简而言之, class<< < object> 打开给定对象的实例类,从而允许为给定实例定义其他实例方法,该方法在其他任何对象上均不可用。

In short, class << <object> opens an instance class for given object, allowing to define additional instance methods for given instance, which will not be available on any other objects.

现在,这么说:在Ruby中,类是Class类的玩笑实例。

Now, with that being said: in Ruby, classes are jest instances of class Class. This:

class A
end

几乎等于(差异在这里并不重要)

Is almost equivalent to (differences not important here)

A = Class.new

因此,如果类是对象,则可以创建其实例类。

So if classes are objects, they can create their instance classes.

class << A
  def foo
  end
end

A.foo  #=> nil
Class.new.foo #=> undefined method

通常用于在给定的类中添加所谓的类方法,但是要点是实际上是在类的实例类上创建实例方法。

It is commonly used to add so-called class method to given class, the point however is that in fact you are creating an instance method on class' instance class.

这篇关于“&lt;&lt;&lt;类别”的目的是什么?类” (尖括号)语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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