From: Jan Svitok Date: 2006-10-26T19:56:01+09:00 Subject: Re: ruby-prof vs. Object#clone On 10/26/06, Victor 'Zverok' Shepelev wrote: > For some reasons I extensively use this technique: > > obj = MyClass.new(...) > obj.instance_eval{ > def my_cool_method > end > } > > #create 1000 clones of the obj, call #my_cool_method > (1..1000).collect{ > clon = obj.clone > clon.my_cool_method > } > > The problem is ruby-prof shows each clone's #my_cool_method as separate one in the report, so I see 1000 lines with 1 calls of #my_cool_method instead of 1 line with 1000 calls. Is it bug or by design? May this problem be result of the fact I use latest Ruby 1.9 version? This is what I've found in the sources: My_cool_method is a singleton instance method (not an ordinary instance method). singleton methods are cloned by creating proxy method, so they are not identical. object.c: rb_obj_clone() class.c : rb_singleton_class_clone() and clone_method() try: o1 = Object.new # this creates singleton method o1.instance_eval { def m ; end } o2 = o1.clone p o1.method(:m) #=> #.m> p o2.method(:m) #=> #(#).m> o3 = o1.clone p o3.method(:m) #=> #(#).m> # now this creates normal method: o1.class.module_eval { def c ; end } p o1.method(:c) #=> # p o2.method(:c) #=> # p o3.method(:c) #=> # However it should be possible to patch ruby-prof to take account for this. IMHO the most simple way is to define normal instance methods via obj.class.module_eval, i.e. MyClass.module_eval. (Please note that I might be wrong, I'm no expert on ruby internals, this is what I learned in a few minutes looking into sources...)