Robert Klemme wrote: >> Try writing the above using the conventional Java-like "blub" >> constructs. You'll have some or all the problems I just mentioned. > > I nevertheless believe there is a more Rubyish solution to achieve what > you did above with new_instance_counted_class: > > module InstanceCounted > module InstanceCounter > attr_accessor :count > end > > def initialize(*a,&b) > super > self.class.count += 1 > end > > def self.included(cl) > cl.extend InstanceCounter > cl.count = 0 > end > end > > class Foo > include InstanceCounted > end I said that one would have some or all the problems I mentioned, not that it couldn't be done. Indeed since you've made an attr_accessor, anyone is free to modify count. Had you used attr_reader instead, anyone could still modify it with instance_eval (as in my first example). Of more importance is the looming conflict with @count. I've spent too much time tracking down the bewildering bugs caused by naming conflicts. No doubt others will think, "Oh, it's so unlikely, don't bother." I used to believe that too. Now I am more careful. I remove all these problems with one fell swoop. -- Posted via http://www.ruby-forum.com/.