zl程序教程

您现在的位置是:首页 >  其他

当前栏目

得到singleton类?

2023-03-14 10:24:48 时间
在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