How to get all class names in a namespace in Ruby?

前端 未结 4 951
挽巷
挽巷 2020-12-28 12:59

I have a module Foo, that it is the namespace for many classes like Foo::Bar, Foo::Baz and so on.

Is there an way to return al

4条回答
  •  萌比男神i
    2020-12-28 13:29

    Foo.constants
    

    returns all constants in Foo. This includes, but is not limited to, classnames. If you want only class names, you can use

    Foo.constants.select {|c| Foo.const_get(c).is_a? Class}
    

    If you want class and module names, you can use is_a? Module instead of is_a? Class.

提交回复
热议问题