From: timr Date: 2011-01-04T01:35:28+09:00 Subject: Re: little tool for learning the ruby lookup path Hi Robert, Thanks for your massive improvements, and insights. I didn't like modifying the object during the investigation, but didn't know about obj#method(m).owner. I can't believe I hadn't seen that before. Your version is beautiful. Thanks for helping, Tim On Jan 3, 1:35 am, Robert Klemme wrote: > On Mon, Jan 3, 2011 at 10:05 AM, timr wrote: > > I have been looking at the process ruby utilizes for method lookup, > > and wrote the code below. It has proven very enlightening to me. I am > > sharing. It allows obj#method_table to be called where one normally > > uses obj#methods. It inserts markers (conspicuous method names) into > > the method array that delimit the source for the listed methods up to > > that point so that it is clear how the method array is composed. See > > the test case at the bottom for clarification. > > > The most thrilling thing I have done this entire year is to monkey- > > patch methods and see them hop to the appropriate section of the > > method_table and disappear from the previous position. > > IMHO monkey patching should not be general practice and stay limited > to particular cases because of the dangers it creates. > > > I'd be interested to hear any feedback interested rubyists have about > > the approach or if anyone has suggestions to refactor the code. > > What I don't like about your approach is that you _modify_ what you > investigate.  Analyzing where a method comes from should not change > any of the classes involved. > > Then, I'd define this method as an instance method of Object because > you want to investigate methods of a particular instance (even if it > is a class or module). > > Finally, for classifying things a Hash seems a much better choice than an Array. > > I'd rather do something like this: > > require 'pp' > > class Object >   def method_table >     Hash.new {|h,k| h[k] = []}.tap do |tbl| >       methods.each do |m| >         tbl[method(m).owner] << m >       end >     end >   end > end > > s = "foo" > def s.bar;123;end > > pp s.method_table > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/