get modules that are in a class?

A

Aaron Smith

is it possible to find out what modules have been included inside of a
class?
 
C

Chris Shea

is it possible to find out what modules have been included inside of a
class?

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
 
T

Travis D Warlick Jr

Aaron said:
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
 
A

Aaron Smith

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.
 
D

dblack

Hi --

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)
 
V

vasudevram

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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top