Module children

C

Camille Roux

Hello,

I'd like to get the list of all the classes contained in a module.
What's the simple way to do that?

Thank you
 
A

August Lilleaas

Hello,

I'd like to get the list of all the classes contained in a module.
What's the simple way to do that?

Thank you

Here's a quick and dirty implementation using ObjectSpace

module LibraryWrapperThing
class UtilityClassA; end
class Fish; end
class Dog; end
end
class NotMe; end

classes = []
ObjectSpace.each_object do |obj|
classes << obj if Module === obj
end

p classes.select {|c| c.name =~ /^LibraryWrapperThing::/ }
# => [LibraryWrapperThing::Dog, LibraryWrapperThing::Fish, ...]
 
J

James Edward Gray II

Hello,

I'd like to get the list of all the classes contained in a module.
What's the simple way to do that?
module Foo; A = 1; class B; end; module C; end end => nil
Foo.constants.find_all { |k| Class === Foo.const_get(k) }
=> ["B"]

Personally, I usually map first:
=> [Foo::B]

Then you can switch the the under loved grep():
=> [Foo::B]

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top