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 = 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.
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 = 0
How do I alias Array.new?
alias orig_new method(‘Array.new’)
this doesn’t work either
orig_new = 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 += 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 = 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 = [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 = Array.new 2, :a
puts “a.class is #{a.class}”
puts “count is #{a.count}”
a << 7
puts “a is #{a.inspect}”
puts “”
b = Array.new 3,3
puts “b.class is #{b.class}”
puts “count is #{b.count}”
b[0] = 7
puts “b is #{b.inspect}”
puts “b[0] is #{b[0]}”
puts “”
c = []
puts “c.class is #{c.class}”
puts “count is #{c.count}”
puts “c is #{c.inspect}”