[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

get modules that are in a class?

warhero

7/8/2007 2:01:00 AM

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

--
Posted via http://www.ruby-....

6 Answers

Chris Shea

7/8/2007 2:40:00 AM

0

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

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

warhero

7/8/2007 2:47:00 AM

0

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

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

Travis D Warlick Jr

7/8/2007 3:10:00 AM

0

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

warhero

7/8/2007 3:16:00 AM

0

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

dblack

7/8/2007 11:33:00 AM

0

vasudevram

7/8/2007 4:55:00 PM

0


>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.dancin...
http://jugad.livej...
http://sourceforge.net/proje...