David A. Black wrote:
> Hi --
> 
> I notice that select without a block returns an enumerator in 1.9. I'm
> wonder if there's any use case for this -- in other words, any reason
> you would ever do this:
> 
>   array.select.some_method
> 
> Also, it means that you get an enumerator instead of an error message
> if you do (presumably by accident) this:
> 
>>> a = [1,2,3,4,5]
> => [1, 2, 3, 4, 5]
>>> puts a.select do |x| x > 3 end
> #<Enumerable::Enumerator:0x3b7fcc>
> 
> I know that something similar will happen with map and others, but if
> there's no real reason to get an enumerator, I'd prefer that select
> complained about not having a block.
> 
> 
> David
> 

David,

I'm sure you've played around with this enough to realize that the 
enumerator returned by a.select somehow (and I don't really get the 
magic that is going on) retains its selection semantics.  So:

a = [1,2,3,4,5]
e = a.select
e.each { |x| x %2 == 1}  # => [1,3,5]

I know that that is not a convincing use-case.  But begin able to create 
an enumerator that does selection allows us to chain enumerators:

a.select.with_index { |x,i| x%2 == 1 and i >= 2 }  # => [3,5]

This, I think, is a pretty convincing reason for select to return an 
Enumerator.

On the other hand, I suspect there are other iterators for which it is 
harder to create a convincing use-case.  I've you've looked at the C 
code for the iterators, you know that all it takes to make an iterator 
(implemented in C) return an Enumerator when no block is passed is to 
insert a single macro call.  My recollection is that these macros were 
inserted into the core iterators very quickly in the fall of 2007.  The 
ease with which the change was made makes me wonder if the use case for 
each iterator was thought through or if it was more a case of ease and 
consistency: might as well make all iterators return Enumerators because 
it is easy and most of them do.

Perhaps there was discussion in Japanese on ruby-dev.  Or maybe Matz has 
a vision that hasn't been fully explained yet :-)  Our recent long 
thread about the proposed with_memo method shows that we (collectively) 
don't yet fully grok enumerators.

	David Flanagan