From: "juan pedro meriƱo" Date: 2007-06-22T05:33:35+09:00 Subject: Re: How do I intercept creation of fundatmental types, eg. [] ------=_Part_153125_19494614.1182458016195 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline How i use pointer in ruby? 2007/6/21, Durant S. : > > > Hi, > > I'm new to ruby and I love the introspection and metaprogramming > capabilities. However, I'm having a problem. I want to count the number > of arrays created. I can only intercept and redefine explicit calls to > Array.new > > For example, I can catch this: > > b =3D Array.new > > but I cannot catch this: > > x =3D [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. > > > Here is a simple program I am using to try to intercept the creation of > Array (I've tried catching "[]", initialize, new and clone - I also > tried overriding Object.new, but couldn't get that to work either, but > that code is not included). You'll notice little newbie commented > questions sprinkled in the code. > > The count variable should be 4 because I make 4 arrays. How do I get > that? > > Thanks in advance! > > > > #! /usr/bin/env ruby > > class Array > > @@count =3D 0 > > # How do I alias Array.new? > # alias orig_new method('Array.new') > > # this doesn't work either > # orig_new =3D method('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 +=3D 1 > puts "\tcount is now #{@@count}" > #orig_new > # Is this right? > super > end > > # this alias does not work > # how do refer to this method "[]"? > # alias orig_ob_cb method('[]') > # def [](*args, &block) > # puts "In [] ... We never seem to get here." > # # this is probably the method for elem =3D array[] > # # a right hand side assignment > # orig_ob_cb(*args, &block) > # end > > alias orig_clone clone > def clone > puts "In clone ... Nope we're not cloning." > orig_clone > end > > alias orig_initialize initialize > def initialize(*args, &block) > puts "An Array was just initialized!" > orig_initialize(*args, &block) > end > > def count > @@count > end > end > > x =3D [3,4,5] > puts "x.class is #{x.class}" > # notice we have the count attribute, but we never called new > puts "count is #{x.count}" > puts "x is #{x.inspect}" > > puts "" > a =3D Array.new 2, :a > puts "a.class is #{a.class}" > puts "count is #{a.count}" > a << 7 > puts "a is #{a.inspect}" > > puts "" > b =3D Array.new 3,3 > puts "b.class is #{b.class}" > puts "count is #{b.count}" > b[0] =3D 7 > puts "b is #{b.inspect}" > puts "b[0] is #{b[0]}" > > puts "" > c =3D [] > puts "c.class is #{c.class}" > puts "count is #{c.count}" > puts "c is #{c.inspect}" > > -- > Posted via http://www.ruby-forum.com/. > > --=20 Juan Pedro Meri=F1o Diaz Ing de Sistema CURN ------=_Part_153125_19494614.1182458016195--