Project

General

Profile

Actions

Feature #14114

open

Add #step for Array, Enumerable, Enumerator

Added by eike.rb (Eike Dierks) over 6 years ago. Updated almost 5 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:83800]

Description

This must have been discussed before,
please reassigned and close this one.

I want to propose an extension to the ruby core api:

  • add #step(n) for Enumerable, Enumerator
  • coerce the step() api across all the api (see Numeric#step(by:n, to:m))
  • extend to: #step((by:n, to:m, offset:k)

We have Range#step,
but #step is not yet defined for Array, Enumerable, Enumerator

I believe, the semantics of #step, as defined in Range
applies like well for Enumerable and Enumerator

It should return every n'th element of the collection.

As of 2.4 #step is not yet defined for Enumerable/Enumerator.
But the semantics of this method is already well defined for Range.

That same semantics should also apply for Enumerable#step(n)
aka: pick every n'th element.

Examples:

('a'..'z').step(2)
=> #<Enumerator: ...>

('a'..'z').to_a.step(2)
NoMethodError: undefined method `step'

('a'..'z').to_enum.step(2)
NoMethodError: undefined method `step'

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0