Introducing Range#% as an alias to Range#step
Combining these new features, we can write an endless step range like
(1..).step(2) in Ruby 2.6.
It can be used for array slicing like python's
Range#% is introduced as an alias to
Range#step, we can write a step range like
This notation is already introduced numo-narray.
range.c: Add Range#%
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
#3 [ruby-core:86609] Updated by marcandre (Marc-Andre Lafortune) 8 months ago
I am not convinced that
step is used enough to justify this. I know I basically never use it. Here's the number of uses for some projects:
rails: 3 uses
bundler: 0 uses
sinatra: 0 uses
WikiEduDashboard: 0 uses (a typical Rails app: https://github.com/WikiEducationFoundation/WikiEduDashboard )
When thinking about this, I am able to see the relation between "modulo" and "step", but it wasn't immediately obvious at all.
In summary: my opinion is that it is not worth the cognitive load.
#4 [ruby-core:86620] Updated by baweaver (Brandon Weaver) 8 months ago
Have we considered a name like
(1..).every(2) # => 2, 4, 6, 8
I did not know that
step could do this until I read this. The name does not clearly indicate that it would do that to me, but that may also be my lack of knowledge of it.
% feels like moving in the opposite direction of clarity. It'd be great for terseness and golf but may be overkill for general usage.
#5 [ruby-core:86656] Updated by mrkn (Kenta Murata) 8 months ago
I'm supposing that this new notation of Range#step is mostly used for slicing numerical arrays like Numo::NArray.
This usecase is very similar to Python's slice notation used for slicing numpy's arrays.
With this new notation,
ary[::2] in python can be written as
ary[(0...)%2] in Ruby 2.6.
Slicing an array with a stepped range is often used in data analysis.
For example, if daru supports this new notation, we can pick up rows of even index in a dataframe
#6 [ruby-core:86809] Updated by marcandre (Marc-Andre Lafortune) 7 months ago
I guess it's a way to make
Enumerator::ArithmeticSequence even more "core", even if it's rarely used.
Has there been discussion of:
#9 [ruby-core:87129] Updated by shevegen (Robert A. Heiler) 7 months ago
I personally am not hugely comfortable with endless Range, but I understand
the reasoning given by mame for it. There is nothing shorter than omission
of characters. :D
Since endless Range was accepted, I think using % on Range, as explained by
mrkn, makes sense too. I personally like step more because it tells me
more (somewhat similar reason for as to why I prefer to not omit an end
range in the ruby code that I write), but I think since endless Range was
accepted, accepting the issue request here makes sense too. (I am not sure
if anyone understood what I was trying to say, but in short, +1 to the
Ruby allows for different paradigms and writing styles and people can
always decide for their own how (and what) to write/code anyway. It's a
similar situation with @@ class variables. One can decide to use them or
not use them. I realized that I don't really need them, so I don't use
them in my own code.