天天看点

得到singleton类?

在ruby中,无论类或者对象,都有一个singlton类(或者称为metaclass),有兴趣的话读读过去写的这篇《ruby的对象模型》。当我们获取某个类或者对象的class属性时,其实会忽略c ruby层次上的singleton类,那么我们如何得到singleton类呢?比较有趣的做法:

singletonclass=class<<test;self;end

对于对象,也是如此:

test=test.new

metaclass=class<<test;self;end

文章转自庄周梦蝶  ,原文发布时间2007-10-31