Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Module children

Reply
Thread Tools

Module children

 
 
Camille Roux
Guest
Posts: n/a
 
      02-01-2010
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
August Lilleaas
Guest
Posts: n/a
 
      02-01-2010
On Feb 1, 8:50*pm, Camille Roux <(E-Mail Removed)> wrote:
> 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
> --
> Posted viahttp://www.ruby-forum.com/.


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:og, LibraryWrapperThing::Fish, ...]
 
Reply With Quote
 
 
 
 
Aaron Patterson
Guest
Posts: n/a
 
      02-01-2010
On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille Roux wrote:
> 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"]

--
Aaron Patterson
http://tenderlovemaking.com/

 
Reply With Quote
 
August Lilleaas
Guest
Posts: n/a
 
      02-01-2010
On Feb 1, 10:06*pm, Aaron Patterson <(E-Mail Removed)>
wrote:
> >> module Foo; A = 1; class B; end; module C; end end

> => nil
> >> Foo.constants.find_all { |k| Class === Foo.const_get(k) }

>
> => ["B"]


+1
 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      02-01-2010

On Feb 1, 2010, at 13:06 , Aaron Patterson wrote:

> On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille Roux wrote:
>> 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.constants.map { |s| Foo.const_get s }.find_all { |k| Class === k }

=> [Foo::B]


 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      02-01-2010
On Feb 1, 2010, at 3:38 PM, Ryan Davis wrote:

>
> On Feb 1, 2010, at 13:06 , Aaron Patterson wrote:
>
>> On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille Roux wrote:
>>> 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.constants.map { |s| Foo.const_get s }.find_all { |k| Class === k }

> => [Foo::B]


Then you can switch the the under loved grep():

>> Foo.constants.map { |s| Foo.const_get s }.grep(Class)

=> [Foo::B]

James Edward Gray II

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      02-01-2010

On Feb 1, 2010, at 13:55 , James Edward Gray II wrote:

> On Feb 1, 2010, at 3:38 PM, Ryan Davis wrote:
>
>> Personally, I usually map first:
>>
>>>> Foo.constants.map { |s| Foo.const_get s }.find_all { |k| Class === k }

>> => [Foo::B]

>
> Then you can switch the the under loved grep():
>
>>> Foo.constants.map { |s| Foo.const_get s }.grep(Class)

> => [Foo::B]


Ah! I always forget about that one. Good catch.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Review: FF7: Advent Children Gez Media 6 11-22-2005 04:06 AM
[OT]No small children... JaR MCSE 0 07-01-2005 07:59 PM
Children's User Accounts =?Utf-8?B?Q2luZHlH?= Microsoft Certification 1 02-13-2005 06:30 PM
Re:YOU ARE A BUNCH OF CHILDREN!!! The Poster Formerly Known as Kline Sphere MCSE 3 01-12-2005 05:26 PM



Advertisments