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


@Eregon, I wanted at first to see what people say about this one :) 

`Array#[]=` is a thing that should be kinda "symmetric", but playing a bit with it, I understood that I am afraid of trying to guess what would be "logical". 

Honestly, I can't remember I've ever used a form like `a[1..3] = 'x'`, and its behavior is kinda "theoretically logical", but at the same time only one of the things you may "intuitively" expect ("replace all three elements with one, changing array's size" wouldn't be my first guess...). 

So, at least for now, my only proposal is `Array#[]`.

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

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
----------------------------------------
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>