如何获得所有定义的类? [英] How can I get all defined classes?

查看:33
本文介绍了如何获得所有定义的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取所有已定义类的列表.我正在寻找这样的代码:

Is there a way to get the list of all defined classes. I'm looking for a code like this:

Class.instances.each do |klass|
  puts klass.name
end

如果没有办法做到这一点.是否可以定义一个类 A 以便在其中注册其后代?比如这样:

If there is no way to do this. It is possible to define a class A such that its descendants are registered in it? For example in this way:

class A
  ...
end

class B < A
end

class C < B
end

A.descendants   # => [B, C] 

推荐答案

p ObjectSpace.each_object(Class){|ob| p ob}

这篇关于如何获得所有定义的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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