类<< Ruby 中的自我习语
class << 是什么意思? self
在 Ruby 中做什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
class << 是什么意思? self
在 Ruby 中做什么?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
首先,
类<< foo
语法打开了foo
的单例类(eigenclass)。这允许您专门化在该特定对象上调用的方法的行为。现在,回答这个问题:
class << self
打开了self
的单例类,以便可以为当前self
对象重新定义方法(在类或模块体内是类或模块本身)。通常,这用于定义类/模块(“静态”)方法:这也可以写为简写:
或更短:
在函数定义内部时,
self
指的是该函数的对象正在被调用。在这种情况下,class << self
打开该对象的单例类;它的一个用途是实现穷人的状态机:因此,在上面的示例中,
StateMachineExample
的每个实例都有process_hook
别名为process_state_1
,但请注意后者如何将process_hook
(仅适用于self
,不影响其他StateMachineExample
实例)重新定义为process_state_2< /代码>。因此,每次调用者调用 process 方法(调用可重新定义的 process_hook)时,行为都会根据其所处的状态而变化。
First, the
class << foo
syntax opens upfoo
's singleton class (eigenclass). This allows you to specialise the behaviour of methods called on that specific object.Now, to answer the question:
class << self
opens upself
's singleton class, so that methods can be redefined for the currentself
object (which inside a class or module body is the class or module itself). Usually, this is used to define class/module ("static") methods:This can also be written as a shorthand:
Or even shorter:
When inside a function definition,
self
refers to the object the function is being called with. In this case,class << self
opens the singleton class for that object; one use of that is to implement a poor man's state machine:So, in the example above, each instance of
StateMachineExample
hasprocess_hook
aliased toprocess_state_1
, but note how in the latter, it can redefineprocess_hook
(forself
only, not affecting otherStateMachineExample
instances) toprocess_state_2
. So, each time a caller calls theprocess
method (which calls the redefinableprocess_hook
), the behaviour changes depending on what state it's in.我找到了关于
class << 的超级简单解释self
、Eigenclass
和不同类型的方法。在 Ruby 中,可以将三种类型的方法应用于类:
实例方法和类方法几乎与其他编程语言中的同名异义相似。
访问
Eigenclass
(包括单例方法)的另一种方法是使用以下语法(class <<
):现在您可以为
定义单例方法self
在此上下文中是类Foo
本身:I found a super simple explanation about
class << self
,Eigenclass
and different type of methods.In Ruby, there are three types of methods that can be applied to a class:
Instance methods and class methods are almost similar to their homonymous in other programming languages.
Another way of accessing an
Eigenclass
(which includes singleton methods) is with the following syntax (class <<
):now you can define a singleton method for
self
which is the classFoo
itself in this context:通常,实例方法是全局方法。这意味着它们在定义它们的类的所有实例中都可用。相反,单例方法是在单个对象上实现的。
Ruby 将方法存储在类中,并且所有方法都必须与类关联。定义单例方法的对象不是类(它是类的实例)。如果只有类才能存储方法,那么对象怎么能存储单例方法呢?创建单例方法时,Ruby 会自动创建一个匿名类来存储该方法。这些匿名类称为元类,也称为单例类或特征类。单例方法与元类相关联,元类又与定义单例方法的对象相关联。
如果在单个对象中定义了多个单例方法,则它们都存储在同一个元类中。
在上面的例子中,
class << z1
改变当前的 self 指向 z1 对象的元类;然后,它在元类中定义 say_hello 方法。类也是对象(称为 Class 的内置类的实例)。类方法只不过是与类对象关联的单例方法。
所有对象都可能有元类。这意味着类也可以有元类。在上面的例子中,class << self 修改 self,使其指向 Zabuton 类的元类。当一个方法在没有显式接收者(将在其上定义该方法的类/对象)的情况下定义时,它会在当前范围内隐式定义,即 self 的当前值。因此,stuff 方法是在 Zabuton 类的元类中定义的。上面的示例只是定义类方法的另一种方式。恕我直言,最好使用 def self.my_new_clas_method 语法来定义类方法,因为它使代码更容易理解。包含上面的示例是为了让我们了解当我们遇到类 << 时会发生什么。自我语法。
其他信息可以在这篇关于 Ruby 类的文章中找到。
Usually, instance methods are global methods. That means they are available in all instances of the class on which they were defined. In contrast, a singleton method is implemented on a single object.
Ruby stores methods in classes and all methods must be associated with a class. The object on which a singleton method is defined is not a class (it is an instance of a class). If only classes can store methods, how can an object store a singleton method? When a singleton method is created, Ruby automatically creates an anonymous class to store that method. These anonymous classes are called metaclasses, also known as singleton classes or eigenclasses. The singleton method is associated with the metaclass which, in turn, is associated with the object on which the singleton method was defined.
If multiple singleton methods are defined within a single object, they are all stored in the same metaclass.
In the above example,
class << z1
changes the current self to point to the metaclass of the z1 object; then, it defines the say_hello method within the metaclass.Classes are also objects (instances of the built-in class called Class). Class methods are nothing more than singleton methods associated with a class object.
All objects may have metaclasses. That means classes can also have metaclasses. In the above example, class << self modifies self so it points to the metaclass of the Zabuton class. When a method is defined without an explicit receiver (the class/object on which the method will be defined), it is implicitly defined within the current scope, that is, the current value of self. Hence, the stuff method is defined within the metaclass of the Zabuton class. The above example is just another way to define a class method. IMHO, it's better to use the def self.my_new_clas_method syntax to define class methods, as it makes the code easier to understand. The above example was included so we understand what's happening when we come across the class << self syntax.
Additional info can be found at this post about Ruby Classes.
什么类<< thing 的作用是:
[它使得
self == thing.singleton_class
在其块的上下文中]。thing.singleton_class是什么?
hi
对象从其#singleton_class.instance_methods
继承其#methods
,然后从其#class.instance_methods
继承。< br>这里我们给出了
hi
的单例类实例方法:a
。这可以通过 class << 来完成。嗨。hi
的#singleton_class
具有hi
的#class
具有的所有实例方法,可能还有更多 (<代码>:a此处)。[事物的实例方法
#class
和#singleton_class
可以直接应用于事物。当 ruby 看到 thing.a 时,它首先在 thing.singleton_class.instance_methods 中查找方法定义,然后在 thing.class.instance_methods 中查找]顺便说一下 - 他们调用对象的单例类 == 元类 == 特征类。
What class << thing does:
[it makes
self == thing.singleton_class
in the context of its block].What is thing.singleton_class?
hi
object inherits its#methods
from its#singleton_class.instance_methods
and then from its#class.instance_methods
.Here we gave
hi
's singleton class instance method:a
. It could have been done with class << hi instead.hi
's#singleton_class
has all instance methodshi
's#class
has, and possibly some more (:a
here).[instance methods of thing's
#class
and#singleton_class
can be applied directly to thing. when ruby sees thing.a, it first looks for :a method definition in thing.singleton_class.instance_methods and then in thing.class.instance_methods]By the way - they call object's singleton class == metaclass == eigenclass.
单例方法是仅为单个对象定义的方法。
示例:
SomeClass 的 Singleton 方法
test
test_obj 的 Singleton 方法
test_2
test_3 >
А singleton method is a method that is defined only for a single object.
Example:
Singleton's methods of SomeClass
test
Singleton's methods of test_obj
test_2
test_3
事实上,如果您为 Ruby 项目编写任何 C 扩展,那么实际上只有一种方法来定义 Module 方法。
我知道这种自助业务只会带来各种其他问题,因此您可以通过搜索每个部分做得更好。
先有对象。
我可以为 foo 创建一个方法吗?
当然
我该怎么办?
只是另一个对象。
您将获得所有对象方法以及新方法。
只是 foo 对象。
尝试看看如果您从其他对象(例如类和模块)创建 foo 会发生什么。所有答案中的示例都很好用,但您必须使用不同的想法或概念才能真正理解代码的编写方式发生了什么。现在您有很多术语需要查看。
单例,
班级,
模块,
自己,
目的,
Eigenclass 被提出,但 Ruby 并没有这样命名对象模型。它更像是元类。
理查德或 __why 在这里向您展示这个想法。
http://viewsourcecode.org/why/hacking/seeingMetaclassesClearly.html
如果这让您大吃一惊,请尝试在搜索中查找 Ruby 对象模型。
我在 YouTube 上知道的两个视频是 Dave Thomas 和 Peter Cooper。他们也试图解释这个概念。戴夫花了很长时间才拿到它,所以不用担心。
我也在努力。不然我为什么会在这里?
谢谢你的提问。
另请查看标准库。它有一个单例模块(仅供参考)。
这很好。
https://www.youtube.com/watch?v=i4uiyWA8eFk
In fact if you write any C extensions for your Ruby projects there is really only one way to define a Module method.
I know this self business just opens up all kinds of other questions so you could do better by searching each part.
Objects first.
Can I make a method for foo?
Sure
What do I do with it?
Just another object.
You get all the Object methods plus your new one.
Just the foo Object.
Try to see what happens if you make foo from other Objects like Class and Module. The examples from all the answers are nice to play with but you have to work with different ideas or concepts to really understand what is going on with the way the code is written. So now you have lots of terms to go look at.
Singleton,
Class,
Module,
self,
Object,
and Eigenclass was brought up but Ruby doesn't name Object Models that way. It's more like Metaclass.
Richard or __why shows you the idea here.
http://viewsourcecode.org/why/hacking/seeingMetaclassesClearly.html
And if the blows you away then try looking up Ruby Object Model in search.
Two videos that I know of on YouTube are Dave Thomas and Peter Cooper. They try to explain that concept too. It took Dave a long time to get it so don't worry.
I'm still working on it too. Why else would I be here?
Thanks for your question.
Also take a look at the standard library. It has a Singleton Module just as an FYI.
This is pretty good.
https://www.youtube.com/watch?v=i4uiyWA8eFk