From: Jeff Mitchell Date: 2004-05-09T08:04:07+09:00 Subject: Re: How to get the last 5 elements of an array? --- Gavin Sinclair wrote: > On Sunday, May 9, 2004, 7:23:12 AM, Jeff wrote: > > (where x == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) > > >> 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] > > I'd happily accept x[10..15] == x[20..25] == x[-20..-15] == [] > > >> 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. > > Yes, it's Weird. > I am suddenly inclined to take a purist view of arrays: either modular arithmetic or no modular arithmetic. As such, there is only one rule to know: all the boundary cases are consistent and predictable. > Take this, though. Let y = [7, 8]. Now what could y[-1..1] mean? > > (-1..1) =~ [-1, 0, 1]. All elements of that range represent fair-game > indices into y. So: > > y[-1..1] == [8, 7, 8] ??? > Since we have the ability to specify sets of integers inside [], I think we are forced to view y as a function. y[-1..1] == [8, 7, 8] is absolutely expected, to me. I put in 3 elements, I expect to get out 3 elements. This is consistent for all cases: put in n, get out n. We can make a truism of it, y[a, b] == y[a..b] == [y[a], .. , y[b]] With the current ruby behavior, can you list all the conditions under which the above holds, and all the conditions and their outcomes when the above doesn't hold? Will you remember 8 months from now, or will you need to fire up irb to test it out again? As you can see, my bias is towards consistency. It's simply amazing how much code is devoted to special cases. That code is ugly and distracting. I'm not particularly purist, but I sure am when it comes to consistent behavior. I am borderline trolling now so I'll stop :) __________________________________ Do you Yahoo!? Win a $20,000 Career Makeover at Yahoo! HotJobs http://hotjobs.sweepstakes.yahoo.com/careermakeover