Module children

Discussion in 'Ruby' started by Camille Roux, Feb 1, 2010.

  1. Camille Roux

    Camille Roux Guest

    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/.
     
    Camille Roux, Feb 1, 2010
    #1
    1. Advertising

  2. On Feb 1, 8:50 pm, 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?
    >
    > 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::Dog, LibraryWrapperThing::Fish, ...]
     
    August Lilleaas, Feb 1, 2010
    #2
    1. Advertising

  3. 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/
     
    Aaron Patterson, Feb 1, 2010
    #3
  4. On Feb 1, 10:06 pm, Aaron Patterson <>
    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 :)
     
    August Lilleaas, Feb 1, 2010
    #4
  5. Camille Roux

    Ryan Davis Guest

    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]
     
    Ryan Davis, Feb 1, 2010
    #5
  6. 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
     
    James Edward Gray II, Feb 1, 2010
    #6
  7. Camille Roux

    Ryan Davis Guest

    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.
     
    Ryan Davis, Feb 1, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Heavey
    Replies:
    0
    Views:
    1,266
    Jim Heavey
    Apr 14, 2004
  2. Lisa Calla
    Replies:
    1
    Views:
    3,579
    Kumar Reddi
    Nov 8, 2004
  3. Mariusz
    Replies:
    0
    Views:
    417
    Mariusz
    Dec 22, 2004
  4. Otis Mukinfus
    Replies:
    0
    Views:
    386
    Otis Mukinfus
    Jan 9, 2005
  5. Maric Michaud
    Replies:
    0
    Views:
    7,222
    Maric Michaud
    Jun 24, 2006
Loading...

Share This Page