Project

General

Profile

Feature #9557

Enumerator#next and Enumerator#peek with argument

Added by sawa (Tsuyoshi Sawada) over 6 years ago. Updated over 6 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:61030]

Description

It often happens that I want to move the current index of an enumerator by some arbitrary number n. Enumerator#feed takes the element as the argument, but that cannot be used if the enumerator has duplicate elements, or when I do not have information of a particular element to choose but just want to increment the index by some number. Enumerator#next, on the other hand, has a fixed value 1 to be incremented. It would be convenient if Enumerator#next takes an optional argument that represents the difference of the index to be incremented. The argument can be understood to be defaulted to 1 when absent.

Also, I often want to look not necessarily the current position, but some position away. It would be good if Enumerator#peek takes an optional argument that represents the positional difference to be peeked. The argument can be understood to be defaulted to 0 when absent.

enum = [0, 1, 2, 3, 4, 5, 6, 7, 8].to_enum
enum.peek    # => 0
enum.peek(0) # => 0
enum.peek(1) # => 1
enum.peek    # => 0
enum.next    # => 0
enum.next(1) # => 1
enum.next(2) # => 2
enum.peek    # => 4
enum.peek(0) # => 4
enum.peek(1) # => 5
enum.peek    # => 4
enum.next    # => 4
enum.next(1) # => 5
enum.next(2) # => 6
peek         # => 8

Also available in: Atom PDF