On Jul 22, 2008, at 10:24 AM, Julien Thewys wrote:

> I want to make a simple module that makes its including classes
> self-countable.
> Here is the code (that does not work, incidentally):


something like ?


cfp:~ > cat a.rb
class Class
   New = method :new unless defined?(New)
   Count = Hash.new{|h,k| h[k] = 0} unless defined?(Count)

   def new *a, &b
     New.call(*a, &b)
   ensure
     Count[self] += 1 unless $!
   end

   def count
     Count[self]
   end
end

class C; end

3.times{ C.new }
3.times{ Array.new }


p 'C.count' => C.count
p 'Array.count' => Array.count
p 'Hash.count' => Hash.count





cfp:~ > ruby a.rb
{"C.count"=>3}
{"Array.count"=>3}
{"Hash.count"=>0}



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