How to get the last 5 elements of an array?

Hi,

I want to extract the last 5 elements of an array ‘x’. I know that x
is an array, but I don’t know how many elements it has. What do you
suggest?

In code, this is what I want.

def last5(arr)
# Fill me in.
end

last5 [] # -> []
last5 [1] # -> [1]
last5 [1,2] # -> [1,2]
last5 [1,2,3,4,5,6,7] # -> [3,4,5,6,7]
last5 nil # -> error

Thanks,
Gavin

Hi Gavin,

ruby -e ‘p [1, 2, 3, 4, 5, 6, 7, 8, 9, 10][-5…-1]’

Martin

···

On Sat, 2004-05-08 at 22:08, Gavin Sinclair wrote:

Hi,

I want to extract the last 5 elements of an array ‘x’. I know that x
is an array, but I don’t know how many elements it has. What do you
suggest?

In code, this is what I want.

def last5(arr)
# Fill me in.
end

last5 # →
last5 [1] # → [1]
last5 [1,2] # → [1,2]
last5 [1,2,3,4,5,6,7] # → [3,4,5,6,7]
last5 nil # → error

Thanks,
Gavin

Gavin Sinclair wrote:

Hi,

I want to extract the last 5 elements of an array ‘x’. I know that x
is an array, but I don’t know how many elements it has. What do you
suggest?

In code, this is what I want.

def last5(arr)

Fill me in.

end

last5 # →
last5 [1] # → [1]
last5 [1,2] # → [1,2]
last5 [1,2,3,4,5,6,7] # → [3,4,5,6,7]
last5 nil # → error

How about …

def last5(arr)
if arr.length < 5
arr.dup
else
arr[-5, 5]
end
end

But [1,2][-5,5] => nil
and Gavin wants [1,2] instead of nil

How about

def last5(arr)
if arr.length<5 then arr else arr[-5,5] end
end
last5
=>
last5 [1]
=> [1]
last5 [1,2]
=> [1, 2]
last5 [1,2,3,4,5,6,7]
=> [3, 4, 5, 6, 7]
last5 nil
NoMethodError: undefined method length' for nil:NilClass from (irb):12:in last5’
from (irb):18

···

On Sat, 08 May 2004 21:14:05 +0900, Martin Stannard wrote:

On Sat, 2004-05-08 at 22:08, Gavin Sinclair wrote:

Hi,

I want to extract the last 5 elements of an array ‘x’. I know that x
is an array, but I don’t know how many elements it has. What do you
suggest?

In code, this is what I want.

def last5(arr)
# Fill me in.
end

last5 # →
last5 [1] # → [1]
last5 [1,2] # → [1,2]
last5 [1,2,3,4,5,6,7] # → [3,4,5,6,7]
last5 nil # → error

Thanks,
Gavin

Hi Gavin,

ruby -e ‘p [1, 2, 3, 4, 5, 6, 7, 8, 9, 10][-5…-1]’

Martin

“Harry Ohlsen” harryo@zip.com.au schrieb im Newsbeitrag
news:409CD011.4040905@zip.com.au…

Gavin Sinclair wrote:

Hi,

I want to extract the last 5 elements of an array ‘x’. I know that x
is an array, but I don’t know how many elements it has. What do you
suggest?

In code, this is what I want.

def last5(arr)

Fill me in.

end

last5 # →
last5 [1] # → [1]
last5 [1,2] # → [1,2]
last5 [1,2,3,4,5,6,7] # → [3,4,5,6,7]
last5 nil # → error

How about …

def last5(arr)
if arr.length < 5
arr.dup
else
arr[-5, 5]
end
end

Not general enough. Rather

irb(main):001:0> class Array;def last(n);size>=n ? self[-n … -1] :
self;end;end
=> nil
irb(main):002:0> [1,2].last 5
=> [1, 2]
irb(main):003:0> [1,2,3,4,5,6,7].last 5
=> [3, 4, 5, 6, 7]

robert

Right. Now should that be necessary? Grabbing the last 5 elements of
an array is the kind of thing that should be easy and obvious in Ruby.

So I propose that Array be changed so that

def last5(arr)
arr[-5…-1]
end

meets the requirements I gave earlier. That is

last5 # →
last5 [1] # → [1]
last5 [1,2] # → [1,2]
last5 [1,2,3,4,5,6,7] # → [3,4,5,6,7]
last5 nil # → error

Any thoughts before I raise an RCR?

Cheers,
Gavin

···

On Saturday, May 8, 2004, 10:24:02 PM, Xavier wrote:

But [1,2][-5,5] =>> nil
and Gavin wants [1,2] instead of nil

How about

def last5(arr)
if arr.length<5 then arr else arr[-5,5] end
end

Doh! That’ll teach me to not read the post carefully.

cheers,

Martin

···

On Sat, 2004-05-08 at 22:24, Xavier wrote:

But [1,2][-5,5] => nil
and Gavin wants [1,2] instead of nil

How about

def last5(arr)
if arr.length<5 then arr else arr[-5,5] end
end
last5
=>
last5 [1]
=> [1]
last5 [1,2]
=> [1, 2]
last5 [1,2,3,4,5,6,7]
=> [3, 4, 5, 6, 7]
last5 nil
NoMethodError: undefined method length' for nil:NilClass from (irb):12:in last5’
from (irb):18

Hi –

“Robert Klemme” bob.news@gmx.net writes:

“Harry Ohlsen” harryo@zip.com.au schrieb im Newsbeitrag
news:409CD011.4040905@zip.com.au…

How about …

def last5(arr)
if arr.length < 5
arr.dup
else
arr[-5, 5]
end
end

Not general enough. Rather

irb(main):001:0> class Array;def last(n);size>=n ? self[-n … -1] :
self;end;end
=> nil
irb(main):002:0> [1,2].last 5
=> [1, 2]
irb(main):003:0> [1,2,3,4,5,6,7].last 5
=> [3, 4, 5, 6, 7]

Isn’t that how Array#last works now?

David

···


David A. Black
dblack@wobblini.net

Hi –

Gavin Sinclair gsinclair@soyabean.com.au writes:

But [1,2][-5,5] =>> nil
and Gavin wants [1,2] instead of nil

How about

def last5(arr)
if arr.length<5 then arr else arr[-5,5] end
end

Right. Now should that be necessary? Grabbing the last 5 elements of
an array is the kind of thing that should be easy and obvious in Ruby.

So I propose that Array be changed so that

def last5(arr)
arr[-5…-1]
end

meets the requirements I gave earlier. That is

last5 # →
last5 [1] # → [1]
last5 [1,2] # → [1,2]
last5 [1,2,3,4,5,6,7] # → [3,4,5,6,7]
last5 nil # → error

Any thoughts before I raise an RCR?

Yes. I don’t think it makes sense for this:

arr[-5…-1].include?(arr[-3])

to be false, since this:

(-5…-1).include?(-3)

is true.

But none of this matters, because Array#last takes an argument :slight_smile:

irb(main):008:0> [1,2,3].last(2)
=> [2, 3]

David

···

On Saturday, May 8, 2004, 10:24:02 PM, Xavier wrote:


David A. Black
dblack@wobblini.net

"Robert Klemme" <bob.news@gmx.net> writes:

irb(main):001:0> class Array;def last(n);size>=n ? self[-n .. -1] :
self;end;end

Isn't that how Array#last works now?

No, for 2 reasons
  * Array#last always return an object Array
  * it dup the return value when size < n

svg% cat b.rb
#!/usr/bin/ruby
class Array
   def lest(n)
      >=n ?self[-n..-1]:self
   end

end

class A < Array
end

a = A.new(2, 1)
p a.lest(1).class
p a.last(1).class
svg%

svg% b.rb
A
Array
svg%

Guy Decoux

“David Alan Black” dblack@wobblini.net schrieb im Newsbeitrag
news:m31xlteqa3.fsf@wobblini.net

Hi –

“Robert Klemme” bob.news@gmx.net writes:

“Harry Ohlsen” harryo@zip.com.au schrieb im Newsbeitrag
news:409CD011.4040905@zip.com.au…

How about …

def last5(arr)
if arr.length < 5
arr.dup
else
arr[-5, 5]
end
end

Not general enough. Rather

irb(main):001:0> class Array;def last(n);size>=n ? self[-n … -1] :
self;end;end
=> nil
irb(main):002:0> [1,2].last 5
=> [1, 2]
irb(main):003:0> [1,2,3,4,5,6,7].last 5
=> [3, 4, 5, 6, 7]

Isn’t that how Array#last works now?

Oh, I didn’t even notice there is Array#last. :slight_smile:

robert

David Alan Black dblack@wobblini.net writes:

But none of this matters, because Array#last takes an argument :slight_smile:

irb(main):008:0> [1,2,3].last(2)
=> [2, 3]

Whoops, I forgot the example that corresponds to what you want to
do:

irb(main):003:0> [1,2,3].last(5)
=> [1, 2, 3]

David

···


David A. Black
dblack@wobblini.net

Any thoughts before I raise an RCR?

Yes. I don’t think it makes sense for this:

arr[-5…-1].include?(arr[-3])

to be false, since this:

(-5…-1).include?(-3)

is true.

Your first condition would be justifiably false if arr == [1,2], even
if I submitted an RCR and it were accepted and implemented.

But none of this matters, because Array#last takes an argument :slight_smile:

irb(main):008:0> [1,2,3].last(2)
=> [2, 3]

Sweet! Learn something new every day. That quells the desire for an
RCR, because I’m not sure of the theoretical ground for such, your
example notwithstanding.

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’.

Thanks,
Gavin

···

On Saturday, May 8, 2004, 10:54:02 PM, David wrote:

Hi –

Gavin Sinclair gsinclair@soyabean.com.au 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.

David

···


David A. Black
dblack@wobblini.net

-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.

···

— David Alan Black dblack@wobblini.net wrote:

Hi –

Gavin Sinclair gsinclair@soyabean.com.au 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.


Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover

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.

···

— Jeff Mitchell quixoticsycophant@yahoo.com wrote:

— David Alan Black dblack@wobblini.net wrote:

Hi –

Gavin Sinclair gsinclair@soyabean.com.au 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.


Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover

(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.

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] ???

That seems wrong! Yet what is the “intuitive” answer? [8,7] or
[7,8]? And why?

So if ranges of this type are going to produce something useful (as
x[-3…2] seems like it ought to), then there would be some pretty
funky rules buried into Array#.

Cheers,
Gavin

···

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 :slight_smile:

···

— Gavin Sinclair gsinclair@soyabean.com.au wrote:

On Sunday, May 9, 2004, 7:23:12 AM, Jeff wrote:


Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover

(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.

I’m not convinced by this. I like consistency as much as you do, but
there are different ways to achieve consistency. The main thing to
consider, perhaps, is complexity. You’re shooting for an ultra-simple
approach, which is admirable, but I don’t think either no-modular or
all-modular are that useful.

And if you want

(m…n).map { |i| arr[i] /* or arr[i % arr.size] */ }

you know where to find it :slight_smile:

At the end of the day, I’ve never needed wraparound functionality
(arr[-4…6]), and I doubt I ever will. In fact, I doubt anyone will.

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]]

That’s the mapping I coded above.

A nitpick: y[a, b] is not intended to be the same as y[a…b].
arr[n,5] means grab the 5 elements starting at index ‘n’.

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?

Needing to fire up irb is one good test of how intuitive something is,
and there are a few Ruby features that cause me to use irb every time
I need them. However, I must accept that as part of life (and
different things are intuitive to different people), and you can turn
the negative into a positive: “Hey, I can just use irb to see what it
does”.

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 :slight_smile:

At the end of the day, I’d like a fairly complicated, but intuitive,
implementation for Array#[m…n]. It ain’t gonna happen, though. The
current implementation is (I’m pretty sure) justified by some logic,
and the implementation I’d like to see treats Array more like an
ordered set than an array.

a = [1, 3, 5, 7]
a[-1…1] # → [7, 1, 3]
a[-10…10] # → [1, 3, 5, 7]
a[-5…-1] # → [1, 3, 5, 7] # The question that
# started this thread.

b = [1, 2]
b[-1…1] # → [2, 1]
# Not [2, 1, 2]

Anyway, like I said, I don’t see a great need for this stuff, although
I would like [-5…-1] to “work” in all cases.

Cheers,
Gavin

···

On Sunday, May 9, 2004, 9:04:07 AM, Jeff wrote:

— Gavin Sinclair gsinclair@soyabean.com.au 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.

I’m not convinced by this. I like consistency as much as you do, but
there are different ways to achieve consistency. The main thing to
consider, perhaps, is complexity. You’re shooting for an ultra-simple
approach, which is admirable, but I don’t think either no-modular or
all-modular are that useful.

And if you want

(m…n).map { |i| arr[i] /* or arr[i % arr.size] */ }

you know where to find it :slight_smile:

At the end of the day, I’ve never needed wraparound functionality
(arr[-4…6]), and I doubt I ever will. In fact, I doubt anyone will.

I have. I wrapped my own replacement version of # and #= to get
that functionality once. For me, it’s the easiest (and, IMHO, clearest
way to get the first and last elements in one swoop:

max, min = values.sort[-1…0]

I also used it when I did my learning exercise, coding the game of life
in ruby. You need to go through each cell in a 2d array, and total the
values of the immediately surrounding cells, wrapping over to the other
end of the array if necessary.


.###.
.#x#.
.###.

… where ‘x’ is the cell you are getting the total for, and ‘#’ are
teh cells you need the values of. the [-1…0] trick is especially handy
for getting the middle row, straddling the center cell.

There are other times I’ve used this, enough to where I have the
function almost memorized from recoding it each time I need it.

add me to the list of people who would like it built in, though :slight_smile: Just
never thought to mention it.

···

On May 8, 2004, at 11:01 PM, Gavin Sinclair wrote:

On Sunday, May 9, 2004, 9:04:07 AM, Jeff wrote:

— Gavin Sinclair gsinclair@soyabean.com.au wrote:

On Sunday, May 9, 2004, 7:23:12 AM, Jeff wrote:

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]]

That’s the mapping I coded above.

A nitpick: y[a, b] is not intended to be the same as y[a…b].
arr[n,5] means grab the 5 elements starting at index ‘n’.

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?

Needing to fire up irb is one good test of how intuitive something is,
and there are a few Ruby features that cause me to use irb every time
I need them. However, I must accept that as part of life (and
different things are intuitive to different people), and you can turn
the negative into a positive: “Hey, I can just use irb to see what it
does”.

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 :slight_smile:

At the end of the day, I’d like a fairly complicated, but intuitive,
implementation for Array#[m…n]. It ain’t gonna happen, though. The
current implementation is (I’m pretty sure) justified by some logic,
and the implementation I’d like to see treats Array more like an
ordered set than an array.

a = [1, 3, 5, 7]
a[-1…1] # → [7, 1, 3]
a[-10…10] # → [1, 3, 5, 7]
a[-5…-1] # → [1, 3, 5, 7] # The question that
# started this thread.

b = [1, 2]
b[-1…1] # → [2, 1]
# Not [2, 1, 2]

Anyway, like I said, I don’t see a great need for this stuff, although
I would like [-5…-1] to “work” in all cases.

Cheers,
Gavin