Help: Get list of modules which are included in the class

C

Chirag Mistry

From: Robert Dober [mailto:[email protected]]
# p C2.ancestors - (Class.new.ancestors + [C2])

Hi

Thanks for the reply. I have looked all the solution but all above
solution also includes modules which are included indirectly. I wants
list of modules which are included directly. See below example.

E.g.
----
module IN
end

module M1
include IN
end

module M2
include IN
end

class C
include M1
include M2
...
end
----
If we try above solution then I will get M1, M2, IN and Kernel. But
I want list which contains M1 and M2 only because these both are
included directly in class C.

Regards
Chirag
 
D

dohzya

Le mercredi 01 août 2007 à 05:44 +0900, Wayne E. Seguin a écrit :
Chirag,

This is how I handle it in my applications:

# lib/ruby_extensions.rb
=======================================
class Module
def included_modules
( ancestors - [ Kernel ] ).select { | x | x.class == Module &&
x != Kernel }
end

def directly_included_modules
ancestors - superclass.ancestors - [self]
end

def inherited_modules
superclass.included_modules - self.directly_included_modules
end
end

=======================================
irb:
=======================================
module Module1; end
=>nil
class Class1; include Module1 end
=>Class1
module Module2; end
=>nil
class Class2 < Class1; include Module2 end
=>Class2
=>[Module1]
Class2.included_modules
=>[Module2, Module1]
Class1.directly_included_modules
=>[Module1]
=>[Module2]
=>[]
Class2.inherited_modules
=>[Module1]

Actually, I also allow for filtering out of other modules than Kernel
but that's not relevant here.

I sincerely hope this helps.

=======================================
(easy irb example paste:)
=======================================
module Module1; end
class Class1; include Module1 end
module Module2; end
class Class2 < Class1; include Module2 end

Class1.included_modules
Class2.included_modules

Class1.directly_included_modules
Class2.directly_included_modules

Class1.inherited_modules
Class2.inherited_modules

There is a problem (him ! again ! Arrrrrrg (Monthy Python dixit (yeah
for Monthy Ruby !!! (sorry, I'll try to stop lisp-mod ;)))))
You should try this :
---
module Module1; end
class Class1; include Module1 end
module Module2; end
class Class2 < Class1; include Module1 ; include Module2 end

Class1.included_modules
Class2.included_modules

Class1.directly_included_modules
Class2.directly_included_modules # see this result

Class1.inherited_modules
Class2.inherited_modules
 
D

dohzya

Etienne,
Thank you very much for this feedback. After thinking about it, here
is my take on this:

We include Module1 in Class1 and so from inheritence this implies
that Class2 does not actually include Module1 directly, since it has
already been included. Hence including Module1 in Class2 is
effectively a no-op.

This makes semantic sense, also: "directly_included_modules" should
be the modules that self includes which the superclass does not. In
this light Module1 is not directly included so my code does what is
expected.

(Also, for the record, Module already has an included_modules method,
but I am overwriting because I don't want Kernel listed for my purposes)

(Thanks to Mark Josef for helping me with this idea)

Thanks :$
Indeed, it's comparable with the difference between 'define' and
'overload' (it isn't?)... moreover it's an overloading with same
method(s) :)
(I'm sorry if I'm repeating anyone, it's hard for me to express myself
and understand correctly in English... I'm working in this way)
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top