From: Farrel Lifson Date: 2006-09-15T18:05:21+09:00 Subject: Re: arbitrary indexes On 15/09/06, MonkeeSage wrote: > Jason Nordwick wrote: > > Of course I don't believe that going from value_at to [] would make > > my code faster, just that it would make it more compact. We started > > using Ruby for some moderately heavy numerics, and I haven't been > > able to be as concise (or productive) as I wish (or thought from what > > others told me). > > So define your own implementation! That's one of the things that makes > working in ruby so productive. > > class Array > def [](*args) > if args.size == 1 > at(*args) > else > values_at(*args) > end > end > end > > a = [1,2,3,4,5,6,7,8,9] > p a[2] # => 3 > p a[2,4,6] #=> [3, 5, 7] > > Regards, > Jordan > > > Be careful though as you've broken some functionality as Array#[] can take two arguments to return the y elements starting at index x. [1,2,3,4,5,6,7][2,3] = [3,4,5] Your modification would make [1,2,3,4,5,6,7][2,3] = [3,4] Farrel