From: Robert Klemme Date: 2007-09-19T16:00:32+09:00 Subject: Re: capturing access to array elements 2007/9/19, mortee : > >> 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.) > > > > Having said that it's probably easier to use Delegator to intercept > > method calls. Maybe there's also another approach to solving this. > > Mortee, what are you trying to achieve? > > Actually it's indifferent if I use Delegator or not. In my scenario, I > get an array of record from a library function, and I want to have those > records automatically extended with a module's methods, without any > extra work on the array's user's part (aside from once handling the > array itself). > > But I don't want to traverse the array right when I do this action in > question on the array: I'd like to leave it that to the time the > elements are actually accessed. This is because I want to have records > added to the array afterwards be extended also; and it may take > time-consuming actions for the "array" to fetch its elements, so I don't > want to access them if not necessary. > > That's why I opted to override its element-accessor methods. I guess the > main principle would be the same whether I override the methods of the > array object in question itself, or I use a delegator object which wraps > the array's methods (currently I use the latter, but I'd prefer the > former anyway). Actually I believe the delegation approach (whether with Delegator or other) is the superior approach. Although not with problems of its own (object identity) wrapping something in order to control access to it seems the most natural way to do it because there is no way left for direct manipulation of the Array (yes, I know that you can work your way around that). My 0.02 EUR. Kind regards robert