From: "FireAphis@..." Date: 2007-07-25T17:45:00+09:00 Subject: Re: Help: Get list of modules which are included in the class On Jul 25, 11:21 am, "Robert Dober" wrote: > On 7/25/07, Chirag Mistry wrote: > > > Hi > > > I want to get a list of modules which are included directly in the class > > in ruby. Example: Suppose below are two modules and a class defined in > > ruby script. > > ------------------- > > module M1 > > ... > > end > > module M2 > > ... > > end > > class MyClass > > include M1 > > include M2 > > ... > > end > > ------------------- > > How can I get list of modules which are included directly in the class? > > Here in above example, M1 and M2 are only included in class MyClass so I > > only want a list which contains modules M1 and M2 for MyClass. If we > > call "MyClass.ancestors" which returns all the modules which are include > > directly or indirectly in this class and all parent class hierarchy that > > exactly I don't want to get. > > > Regards > > Chirag > > -- > > Posted viahttp://www.ruby-forum.com/. > > Maybe my friend irb can help ;) > irb(main):001:0> module M1; end > => nil > irb(main):002:0> class C1; include M1 end > => C1 > irb(main):003:0> module M2; end > => nil > irb(main):004:0> class C2 < C1; include M2 end > => C2 > irb(main):017:0> class Module > irb(main):018:1> def included_modules > irb(main):019:2> ancestors.select{|x|x.class==Module} > irb(main):020:2> end > irb(main):021:1> end > irb(main):022:0> C1.included_modules > => [M1, Kernel] > irb(main):023:0> C2.included_modules > => [M2, M1, Kernel] > > You can get of Kernel of course if you do not want it to show up at the party. > HTH > Robert > > -- > I always knew that one day Smalltalk would replace Java. > I just didn't know it would be called Ruby > -- Kent Beck I don't think that it answers the question. Look at the following example: irb(main):120:0> module M1 irb(main):121:1> end => nil irb(main):122:0> module M2 irb(main):123:1> include M1 irb(main):124:1> end => M2 irb(main):125:0> class C1 irb(main):126:1> include M2 irb(main):127:1> end => C1 irb(main):128:0> C1.ancestors.select{|x|x.class==Module} => [M2, M1, Kernel] I think that Chirag wanted to get only M2 and as you can see we get M1 as well. 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? FireAphis