Issue #16818 has been updated by shevegen (Robert A. Heiler).


I somewhat agree with sawa's comment that % on Range may confuse some ruby
folks; I think % is more typically the modulo operator? I can not say how
strong this confusion may be, perhaps small, perhaps not, but I concur with
his original comment in regards to %.

However had, I also happily admit that I really have no particularly strong
opinion either way. I am also indifferent (or, more accurately, a bit 
clueless) about /. Since it is quite unlikely for me to use % or / on 
Range anyway (I tend to stick to oldschool matz-ruby whenever possible ;)),
I let those folks who make use of the range operation, or those who 
proposed it, to comment on that either way since I am not really attached
to that operation to begin with.

It should be pointed out that this has follow-up effects - see zverok's 
example of extending it towards Arithmetic* (and there I also don't have
a big opinion either way, just wanted to point out it that whatever way
is chosen, it may be good to decide on it before ruby 3.0 release I
think).

----------------------------------------
Feature #16818: Rename `Range#%` to `Range#/`
https://bugs.ruby-lang.org/issues/16818#change-85295

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
`Range#%` was introduced as an alias of `Range#step` by 14697, but it is counter-intuitive and confusing.

Iteration in the following:

```ruby
((5..14) % 3).each{|i| p i}
#>> 5
#>> 8
#>> 11
#>> 14
```

is not based on `x % y` in any sense. In fact, actually applying `% 3` to the selected elements returns a unique value `2`, and it is not obvious how this is related to the iteration.

```ruby
[5, 8, 11, 14].map{|i| i % 3}
# => [2, 2, 2, 2]
```

Rather, the concept seems to be based on `/`. Applying `/ 3` to the relevant elements returns a sequence `1, 2, 3, 4`.

```ruby
[5, 8, 11, 14].map{|i| i / 3}
# => [1, 2, 3, 4]
```

Hence, `(5..14).step(3)` can be interpreted like this: Iterate over the [equivalence class](https://en.wikipedia.org/wiki/Equivalence_class) (quotient set) of range `5..14` yielded by `/ 3`.

Notice that the number of elements in `[5, 8, 11, 14]` is 4, which is `(14 - 5 + 1) / 3.0).ceil`, but is not related to `%`.

So I propose that the alias of `Range#step` should be `Range#/`, and `Range#%` should be deprecated as soon as possible before its use accumulates:

```ruby
((5..14) / 3).each{|i| p i}
#>> 5
#>> 8
#>> 11
#>> 14
```

---

P.S.

And if `Range#%` were to be introduced at all, I would rather expect it to behave like the following:

```ruby
((5..14) % 3).each{|i| p i}
#>> 5
#>> 6
#>> 7
```

which is why I claimed above that the current `Range#%` is confusing.



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