Google Groups Home
Help | Sign in
Message from discussion get modules that are in a class?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Travis D Warlick Jr  
View profile
 More options Jul 7 2007, 11:10 pm
Newsgroups: comp.lang.ruby
From: Travis D Warlick Jr <warli...@operissystems.com>
Date: Sun, 8 Jul 2007 12:10:00 +0900
Subject: Re: get modules that are in a class?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google