Forum: Ruby get modules that are in a class?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aaron S. (Guest)
on 2007-07-08 06:00
is it possible to find out what modules have been included inside of a
class?
Wayne E. Seguin (Guest)
on 2007-07-08 06:16
(Received via mailing list)
On Jul 07, 2007, at 22:00 , Aaron S. 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
Aaron S. (Guest)
on 2007-07-08 06:47
Wayne E. Seguin wrote:
> On Jul 07, 2007, at 22:00 , Aaron S. 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
Wayne E. Seguin (Guest)
on 2007-07-08 07:07
(Received via mailing list)
On Jul 07, 2007, at 22:47 , Aaron S. wrote:
> end
>
> t = Test.new
> puts t.included_modules

Aaron,

you can define a method within your class to expose this like so:

module TestModule
   def say_something
     puts "SOMETHING"
   end
end

class Test
   include TestModule
   def modules
     self.class.included_modules
   end
end

t = Test.new
puts t.modules


If you need this exposed for all classes you can expose for the
Object class:

class Object
   def modules
     self.class.included_modules
   end
end

Does this help?
Without knowing what you're trying to accomplish further it's
difficult to come up with an optimal solution :)
Travis D Warlick Jr (Guest)
on 2007-07-08 07:10
(Received via mailing list)
Aaron S. wrote:
>
> 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
Aaron S. (Guest)
on 2007-07-08 07:15
> 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.
Wayne E. Seguin (Guest)
on 2007-07-08 07:18
(Received via mailing list)
On Jul 07, 2007, at 23:06 , Wayne E. Seguin wrote:
> class Test
> If you need this exposed for all classes you can expose for the
> difficult to come up with an optimal solution :)
Aaron,

Scratch that last, it's far easier than that:

module TestModule
   def say_something
     puts "SOMETHING"
   end
end

class Test
   include TestModule
end

t = Test.new
puts t.class.included_modules
unknown (Guest)
on 2007-07-08 15:33
(Received via mailing list)
Hi --

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

>> What about within a class?
>>
>> 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
vasudevram (Guest)
on 2007-07-11 17:20
(Received via mailing list)
>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
This topic is locked and can not be replied to.