Edward Faulkner <ef / alum.mit.edu> writes:

> On Thu, Jan 12, 2006 at 02:40:40AM +0900, Christer Nilsson wrote:
>> Is it possible to catch index out of range ?
>> I managed to redefine Array, but I want to have both possibilities.
>
> You can use a delegator:
>
> require 'delegate'
>
> class SafeArray < DelegateClass(Array)
>   def [](index)
>     raise "index error" if index.abs >= size
>     super(index)
>   end
> end

Why do you use a delegator and don't just directly inherit from Array?

> regards,
> Ed
-- 
Christian Neukirchen  <chneukirchen / gmail.com>  http://chneukirchen.org