Project

General

Profile

Feature #10880

Subclassing Array from Enumerator

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

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

Description

It often happens that I want to use a method from the Enumerator class on an array, and so I frequently use Array#to_enum. Since Array and Enumerator share the same parent Enumerable, they should be very close. I think it would be convenient if Array inherits from Enumerator. (Perhaps the same thing can be said for Hash too)

History

Updated by marcandre (Marc-Andre Lafortune) over 4 years ago

they should be very close

They are very close:

Enumerator.instance_methods - Array.instance_methods
# => [:with_index, :with_object, :next_values, :peek_values, :next, :peek, :feed, :rewind] 

Which method(s) in there do you claim to use frequently?

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

Marc-Andre Lafortune wrote:

Enumerator.instance_methods - Array.instance_methods
# => [:with_index, :with_object, :next_values, :peek_values, :next, :peek, :feed, :rewind] 

Which method(s) in there do you claim to use frequently?

I use with_index and with_object frequently.

Updated by marcandre (Marc-Andre Lafortune) over 4 years ago

I use with_index and with_object frequently.

Any reason why you wouldn't simply use each_with_index and each_with_object?

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

Marc-Andre Lafortune wrote:

I use with_index and with_object frequently.

Any reason why you wouldn't simply use each_with_index and each_with_object?

I want to provide an optional starting number (most often 1) to the former.

But you are making me realize that my motivation for the proposal is getting weaker.

Also available in: Atom PDF