get modules that are in a class?

Discussion in 'Ruby' started by Aaron Smith, Jul 8, 2007.

  1. Aaron Smith

    Aaron Smith Guest

    Aaron Smith, Jul 8, 2007
    #1
    1. Advertising

  2. Aaron Smith

    Chris Shea Guest

    On Jul 7, 8:00 pm, Aaron Smith <> wrote:
    > is it possible to find out what modules have been included inside of a
    > class?
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    This appears to do the trick:

    mvb:~ cms$ irb
    001:0> class C
    002:1> end
    nil
    003:0> C.ancestors.select {|a| a.class == Module}
    [Wirble::Shortcuts, PP::ObjectMixin, Kernel]

    You can tell I require wirble and pp in my irbrc.

    HTH,
    Chris
     
    Chris Shea, Jul 8, 2007
    #2
    1. Advertising

  3. Aaron Smith

    Aaron Smith Guest

    Wayne E. Seguin wrote:
    > On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
    >> is it possible to find out what modules have been included inside of a
    >> class?

    >
    >
    > Yes Aaron it is, via the method "included_modules"
    >
    > http://www.ruby-doc.org/core/classes/Module.html#M001697


    What about within a class?

    module TestModule
    def say_something
    puts "SOMETHING"
    end
    end

    class Test
    include TestModule
    end

    t = Test.new
    puts t.included_modules

    --
    Posted via http://www.ruby-forum.com/.
     
    Aaron Smith, Jul 8, 2007
    #3
  4. Aaron Smith wrote:
    > Wayne E. Seguin wrote:
    >> On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
    >>> is it possible to find out what modules have been included inside of a
    >>> class?

    >>
    >> Yes Aaron it is, via the method "included_modules"
    >>
    >> http://www.ruby-doc.org/core/classes/Module.html#M001697

    >
    > What about within a class?
    >
    > module TestModule
    > def say_something
    > puts "SOMETHING"
    > end
    > end
    >
    > class Test
    > include TestModule
    > end
    >
    > t = Test.new
    > puts t.included_modules
    >


    Remember that when you include the module, you're including _all_ the
    module's methods (including Module#included_modules)

    So, use self.class to get the Class object of the current instance (this
    will work from the included modules also), so you should be able to do:

    self.class.included_modules

    And a debugging efficiency tip: use the Array#sort method with the
    Module#included_modules to sort the list of included modules for easier
    viewing. I use this all the time in IRB. (This also works with
    Class#methods and all the like)

    irb(main):001:0> YourClass.methods.sort

    --
    Travis Warlick
    Operis Systems, LLC
    Lead Developer
     
    Travis D Warlick Jr, Jul 8, 2007
    #4
  5. Aaron Smith

    Aaron Smith Guest

    > And a debugging efficiency tip: use the Array#sort method with the
    > Module#included_modules to sort the list of included modules for easier
    > viewing. I use this all the time in IRB. (This also works with
    > Class#methods and all the like)
    >
    > irb(main):001:0> YourClass.methods.sort


    Thanks. That's perfect.

    --
    Posted via http://www.ruby-forum.com/.
     
    Aaron Smith, Jul 8, 2007
    #5
  6. Aaron Smith

    Guest

    Hi --

    On Sun, 8 Jul 2007, Travis D Warlick Jr wrote:

    > Aaron Smith wrote:
    >> Wayne E. Seguin wrote:
    >>> On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
    >>>> is it possible to find out what modules have been included inside of a
    >>>> class?
    >>>
    >>> Yes Aaron it is, via the method "included_modules"
    >>>
    >>> http://www.ruby-doc.org/core/classes/Module.html#M001697

    >>
    >> What about within a class?
    >>
    >> module TestModule
    >> def say_something
    >> puts "SOMETHING"
    >> end
    >> end
    >>
    >> class Test
    >> include TestModule
    >> end
    >>
    >> t = Test.new
    >> puts t.included_modules
    >>

    >
    > Remember that when you include the module, you're including _all_ the
    > module's methods (including Module#included_modules)


    It's not exactly an inclusion thing. Class objects already respond to
    #included_modules, because Class inherits from Module.

    > So, use self.class to get the Class object of the current instance (this
    > will work from the included modules also), so you should be able to do:
    >
    > self.class.included_modules


    That will work with any object:

    "".class.included_modules

    etc. It's not dependent on your having included a module.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Jul 8, 2007
    #6
  7. Aaron Smith

    vasudevram Guest


    >YourClass.methods.sort


    Yes, that's a useful trick. I use it all the time.
    We can also make up many more such, with a bit of thought.

    Another one I use a lot, when I think that some class is likely to
    have a method with some substring in its name, is:

    YourClass.methods.grep /substring/

    e.g. : String.methods.grep /case/ # to find out what the String method
    name to uppercase (or lowercase) a string, is called.
    or
    "".methods.grep /case/

    Vasudev Ram
    http://www.dancingbison.com
    http://jugad.livejournal.com
    http://sourceforge.net/projects/xtopdf
     
    vasudevram, Jul 8, 2007
    #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. E11
    Replies:
    1
    Views:
    4,842
    Thomas Weidenfeller
    Oct 12, 2005
  2. Remy Cool
    Replies:
    1
    Views:
    437
    Remy Cool
    Aug 27, 2003
  3. Tobiah
    Replies:
    2
    Views:
    320
    Tobiah
    Sep 14, 2003
  4. Ben Weintraub

    Disabling modules using Modules/Setup

    Ben Weintraub, Sep 9, 2006, in forum: Python
    Replies:
    0
    Views:
    359
    Ben Weintraub
    Sep 9, 2006
  5. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    307
    Peter Peyman Puk
    Mar 19, 2010
Loading...

Share This Page