From: Jeff Mitchell Date: 2004-05-09T06:23:12+09:00 Subject: Re: How to get the last 5 elements of an array? --- Jeff Mitchell wrote: > > --- David Alan Black wrote: > > Hi -- > > > > Gavin Sinclair writes: > > > > > For example, consider this: > > > > > > x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > > x[-5..3] # -> nil > > > > > > If even *that* returns nil, when *all* numbers in the range are valid > > > indexes, then... > > > > > > Also, there's everybody's favourite example: > > > > > > x[10..15] # -> [] > > > x[11..15] # -> nil > > > > > > I'd rather see 'arr[range]' *always* return an array, no matter the > > > value of 'arr' and 'range'. > > > > I guess it comes down to the necessity to translate between the very > > different perspectives of Arrays and Ranges when it comes to counting. > > For example, for a 5-element array, -3 > 1, counting Array-style. I > > don't know whether there's a way to un-anomalize it; it may be one of > > these lumps that can be moved around under the carpet but is still > > always somewhere. > > > > -3 == 2 > 1 (mod 5) > > Yes the anomaly is that ruby only modularizes indices i for > -length <= i < length. A purist might say modular arithmetic > should be applied globally or not at all, but this would give > > x[10..15] == x[20..25] == x[-20..-15] == [1,2,3,4,5,6] > > which could fail to indicate Something's Wrong. I see ruby's > subset of modular arithmetic as a middle ground between convenience > and saftey. One can always write ModArray trivially, after all. Whoa hang on, I didn't read the OP well enough. At least x[-3..2] should return [8, 9, 10, 1, 2, 3], otherwise this isn't even a subset of modular arithmetic, it's something else. __________________________________ Do you Yahoo!? Win a $20,000 Career Makeover at Yahoo! HotJobs http://hotjobs.sweepstakes.yahoo.com/careermakeover