From: Robert Dober Date: 2007-07-25T17:21:35+09:00 Subject: Re: Help: Get list of modules which are included in the class 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 via http://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