On Sep 18, 11:10 am, mortee [email protected] wrote:
So which methods in Array do I have to override, if I want to make sure
I can capture any attempt to access an array element? In other words,
which are the Array methods which don’t rely on other ones to access
elements?
Unfortunately, there isn’t a single method that accesses the value at
a particular index. For example, see the following code. Note that the
#[] method isn’t called by the other methods, including each. (Note
also that the following is not an exhaustive list, but does give you a
starting point for experimentally determining which method might call
others.)
a = %w| a b c |
def a.[]( *args, &blk )
p ‘[]’
super *args, &blk
end
def a.each( *args, &blk )
p ‘each’
super *args, &blk
end
def a.select( *args, &blk )
p ‘select’
super *args, &blk
end
def a.delete( *args, &blk )
p ‘delete’
super *args, &blk
end
def a.fetch( *args, &blk )
p ‘fetch’
super *args, &blk
end
puts “Using []:”
a[ 0 ]
puts “-”*40
puts “Using each:”
a.each{ |x| }
puts “-”*40
puts “Using select:”
a.select{ |x| }
puts “-”*40
puts “Using delete:”
a.delete ‘b’
puts “-”*40
puts “Using fetch:”
a.fetch 1