Issue #16812 has been updated by zverok (Victor Shepelev).


> But a negative start with a non-negative end is quite weird. What is that operation even supposed to mean? What is it useful for?

I believe such edge cases might emerge not being directly written, but when dynamically calculated. Imagine calculating some anchor element, and then taking N elements around it. Then, you have, say...
```ruby
def around_mean(ary, count: 3)
  i = ary.index(ary.sum / ary.length)
  ary[i-count..i+count]
end
around_mean((1..20).to_a)
# => [7, 8, 9, 10, 11, 12, 13] 
around_mean((1..6).to_a)
# => [6] -- hm, it is a bit strange
around_mean((1..6).to_a, count: 10)
# => nil -- hm, it is even weirder...
```
The example before last is `[1, 2, 3, 4, 5, 6][-1..5]` which "intuitively weird", as one _might_ expect something like:
```
  1 2 3 4 5 6
^^^^^^^^^^^
```
The very last example is `[1, 2, 3, 4, 5, 6][-8..12]` -- and it even doesn't produce empty array (I pointed at this at #16822, too).

That's not the best possible example, but at least it demonstrates how we can arrive at edge case situation and why we (probably) might expect different behavior here.

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-86359

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
I believe when concepts of ArithmeticSequence and `Range#%` were introduced, one of the main intended usages was array slicing in scientific data processing. So, it seems to make sense to allow this in `Array#[]`:

```ruby
ary[(5..20) % 2] # each second element between 5 and 20
ary[(0..) % 3] # each third element
ary[10.step(by: -1)] # elements 10, 9, 8, 7 ....
```

PR is [here](https://github.com/ruby/ruby/pull/3055).

My reasoning is as follows:
1. As stated above, ArithmeticSequence and `Range#%` seem to have been introduced exactly for this goal
2. Python has its slicing syntax as `begin:end:step` (with a possibility to omit either), and it seems to be well respected and used feature for data processing. So I believe it is useful, and relatively easy to integrate into existing functionality

I expect the usual "it is ugly and unreadable!" backlash. 
I don't have an incentive, nor energy, to "defend" the proposal, so I would not.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>