From: David Alan Black Date: 2001-11-01T07:20:06+09:00 Subject: [ruby-talk:24039] Re: No public methods in 'Object' ? Hello -- On Thu, 1 Nov 2001, Johan Holmberg wrote: > > On Thu, 1 Nov 2001, ts wrote: > > > > > $ ruby -e 'p Object.instance_methods(1).length' > > > 37 > > > > pigeon% ruby -e 'p Kernel.instance_methods.length' > > 37 > > pigeon% > > > > pigeon% ruby -e 'p Object.instance_methods(1).length' > > 37 > > pigeon% > > > > > > Kernel is a module which is included in Object, and where are defined > > instance methods of Object > > > > Thanks. > Now when I reread about 'Object' in the Pickaxe-book I see that > the inclusion of 'Kernel' is mentioned there. > > One of the reasons why I asked my original question is that I'm > trying to figure out how to tell where each public method in a class > is defined. I would like to be able to do: > > some_class = String # for example > > some_class.instance_methods(1).each { |m| > defined_in = .... > puts "method #{m} is defined in #{defined_in}" > } > > I can't figure out what to write instead of "....". > Is there some way to do this without duplicating Ruby's own > search-algorithm among the ancestor classes and modules ? This might be too simplistic for your purposes, but anyway... some_class.instance_eval do public_instance_methods(true).sort.each do |im| defined_in = ancestors.reverse.find do |anc| anc.instance_methods.include?(im) end p "#{im} defined_in #{defined_in}" end end David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav