在Ruby中,无论类或者对象,都有一个singlton类(或者称为metaclass),有兴趣的话读读过去写的这篇《
Ruby的对象模型》。当我们获取某个类或者对象的class属性时,其实会忽略c ruby层次上的singleton类,那么我们如何得到singleton类呢?比较有趣的做法:
对于对象,也是如此:
singletonclass
=
class
<<
Test;self;end
对于对象,也是如此:
test
=
Test.
new
metaclass=class << test;self;end
metaclass=class << test;self;end
文章转自庄周梦蝶 ,原文发布时间 2007-10-31