From: Robert Dober Date: 2008-07-26T02:11:00+09:00 Subject: Re: simple module for "count my instances" behaviour On Fri, Jul 25, 2008 at 6:40 PM, ara.t.howard wrote: > > On Jul 25, 2008, at 7:22 AM, Julien Thewys wrote: > >> I meant other than for hacking purpose. > > > > there are many good reasons - one is for subclassing, if you override new > clients do not have to remember to call 'super' > > class C > def C.new *a, &b > (object = allocate).instance_eval do > init *a, &b > initialize *a, &b > self > end > end > > def init > @you = 'do not have to remember to call this' > end > end > > > class D < C > def initialize > end > end > > > another reason is for instance in the case of dike.rb, by re-defining 'new' > it can track object creation, register a finalizer, and thereby track the > object lifecycle to detect memory leaks > > regards. > > a @ http://codeforpeople.com/ > -- > we can deny everything, except that we have the possibility of being better. > simply reflect on that. > h.h. the 14th dalai lama > > > > > Ara I know your code is quite fine and demonstrating what you wanted to, I am however still not sure that I brought my point across :( The dilemma of a way to redefine new statically (1) or dynamically (2) is to allow it to tolerate other new implementations in the inheritance chain. (1) class C def self.new *args, &blk super(*args, &blk).instance_eval do ... self end end end (2) module M def self.inherited a_mod return if Class === a_mod class << a_mod alias_method :__old__, :new rescue nil self end.module_eval do def new *args, &blk __old__(*args,&blk).instance_eval do ... self end* Cheers Robert -- http://ruby-smalltalk.blogspot.com/ There's no one thing that's true. It's all true. -- Ernest Hemingway