From: "Durant S." Date: 2007-06-22T08:23:59+09:00 Subject: Re: How do I intercept creation of fundatmental types, eg. [ MenTaLguY wrote: > On Fri, 22 Jun 2007 05:28:24 +0900, "Durant S." > wrote: >> For example, I can catch this: >> >> b = Array.new >> >> but I cannot catch this: >> >> x = [3,4,5] >> >> It seems like I should be able to do it with the awesome power of ruby >> :), but I can't figure out how. > > It isn't possible without modifying the interpreter; in the latter case, > the array is created directly by the interpreter, rather than via a > method call. > > -mental Ah, that's too bad. I was encouraged by seeing the count method show up in the "x = [3,4,5]" example Thanks, all. ps - I figured out how to save and call Array.new below. Fortunately, though I'm not sure why, this doesn't cause infinite recursion. class Array @@count = 0 orig_new = %s{Array.new} def Array.new(*args,&block) print "In Array.new" print " with #{args.inspect}" if args print " and #{block.inspect}" if block print "\n" print "\tself.class is #{self.class}\n" @@count += 1 puts "\tcount is now #{@@count}" #super orig_new(*args,&block) end # ... end -- Posted via http://www.ruby-forum.com/.