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

From: Robert D. [mailto:[email protected]]

On 7/25/07, [email protected] [email protected] wrote:

> I think that Chirag wanted to get only M2 and as you can

see we get M1 as well.

What!!! Do I have to read the post in oder to answer now???

My bad you are right indeed.

> I personally think that there is no way to solve this. As far

> as I know Ruby holds its ancestors internally in a plain list and

> doesn’t remember the hierarchy. Can someone tell more on

this matter?

That is indeed more challenging :slight_smile:

FredSenault’s previous post should give a hint

kind regards -botp

Le mercredi 25 juillet 2007 à 20:04 +0900, FireAphis a écrit :

That’s because C2.superclass => Object

:frowning:

FireAphis

You’re right, but it can be fixed :slight_smile:

class Class
def included_modules
c = self
all = c.ancestors - [c]
bad = all.map {|a|
a.ancestors - [a]
}
# Module#superclass doesn’t exist
bad << c.superclass if c.is_a? Class
all - bad.flatten
end
end

module M1; end
module M2; include M1; end
module M3; end
module M4; include M3; end
module M5; end
module M6; include M5; end
module M7; end
class C1; include M2; end
class C2<C1; include M4; end
class C3<C2; include M6; include M7; end

p C3.included_modules # => [M7, M6]


sorry if I’ve sent 3 messages, I’m fighting with my mail client…

On Jul 25, 12:42 pm, dohzya [email protected] wrote:


Etienne Vallette d’Osia
There is another problem. If C2 doesn’t inherit another class but does
include the modules and those modules by themselves include modules
then your code, still, will return the whole hierarchy. Look at this:

module M1; end
module M2; include M1; end
class C2; include M2; end

p (C2.ancestors - [C2] - C2.superclass.ancestors) # => [M1, M2]

That’s because C2.superclass => Object

:frowning:

FireAphis

On 7/25/07, dohzya [email protected] wrote:

That’s because C2.superclass => Object

:frowning:

FireAphis

You’re right, but it can be fixed :slight_smile:
no it cannot :(, look at C4 below
all - bad.flatten
class C1; include M2; end
class C2<C1; include M4; end
class C3<C2; include M6; include M7; end
class C4<C2; include M5; include M6; end


M5 will not show up.

Le mercredi 25 juillet 2007 à 22:58 +0900, Robert D. a écrit :

# Module#superclass doesn't exist

module M6; include M5; end
module M7; end
class C1; include M2; end
class C2<C1; include M4; end
class C3<C2; include M6; include M7; end
class C4<C2; include M5; include M6; end


M5 will not show up.

This was the first and not fixable (with this solution) problem :slight_smile:
module re-included are hidden


module M1; end
module M2; end
module M3; include M2; end
class C1; include M1; end
class C2 < C1; include M1; include M2; include M3; end

only [M3] will show up

too bad…

On 7/25/07, dohzya [email protected] wrote:

}

module M5; end
module re-included are hidden
too bad…
yeah the information just is not in the ancestor array :frowning:
but it was a nice try :wink:

Le 25 juillet à 11:32, Peña, Botp a écrit :

FredSenault’s previous post should give a hint

Just Fred is all right, y’know… :slight_smile:

But this is just to say I missed the obvious with meta-programming
inside the Module class :

class Module
alias orig_include include
def directly_included_modules
@d_i_m || []
end
def include(m)
@d_i_m ||= []
@d_i_m << m
orig_include m
end
end

(Maybe some 'freeze’ing could be in order, too.)

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs