A
Aaron Smith
is it possible to find out what modules have been included inside of a
class?
class?
is it possible to find out what modules have been included inside of a
class?
Wayne said:Yes Aaron it is, via the method "included_modules"
http://www.ruby-doc.org/core/classes/Module.html#M001697
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
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
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
YourClass.methods.sort
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.