# Inconsistency - map/collect not returning enumerator

**URL:** <https://www.ruby-forum.com/t/inconsistency-map-collect-not-returning-enumerator/182396>\
**Category:** Ruby\
**Created:** [January 27, 2010, 2:29pm UTC](https://www.ruby-forum.com/t/inconsistency-map-collect-not-returning-enumerator/182396 "2010-01-27T14:29:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [January 27, 2010, 2:29pm UTC](https://www.ruby-forum.com/t/inconsistency-map-collect-not-returning-enumerator/182396/1 "2010-01-27T14:29:04Z")

</div>

This seems a bit of an anomoly:

> > x = [1,2,3].map  
> > x.each { |y| y\*2 }  
> > =\> [1, 2, 3] # I was expecting [2, 4, 6]

It seems that Array#map and Enumerable#map without a block return just  
an Array. However most other Enumerable methods return an Enumerator,  
even #each (which is pretty useless, since the Enumerator maps #each to  
#each)

> > RUBY\_DESCRIPTION  
> > =\> “ruby 1.8.7 (2009-06-12 patchlevel 174) [x86\_64-linux]”

> > [1,2,3].each  
> > =\> #Enumerable::Enumerator:0x7fa4170ea1b0

> > [1,2,3].map  
> > =\> [1, 2, 3]
> 
> > [1,2,3].collect  
> > =\> [1, 2, 3]

> > class Foo; include Enumerable; def each; yield 1; end; end  
> > =\> nil
> 
> > Foo.new.map  
> > =\> [1]

Anybody got a good explanation for this?

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [January 27, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/inconsistency-map-collect-not-returning-enumerator/182396/2 "2010-01-27T16:10:09Z")

</div>

On Wed, Jan 27, 2010 at 8:29 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > RUBY\_DESCRIPTION  
> > > =\> “ruby 1.8.7 (2009-06-12 patchlevel 174) [x86\_64-linux]”

> Anybody got a good explanation for this?

Yes. You’re using Ruby 1.8.7, which is, in and of itself, an anomaly 🙂

But this may be to preserve compatibility with Ruby 1.8.6:

> > RUBY\_VERSION  
> > =\> “1.8.6”

# NOT OK TO CHANGE BEHAVIOR

> [1,2,3].map  
> =\> [1, 2, 3]

# OK TO ADD BEHAVIOR

> > [1,2,3].select  
> > LocalJumpError: no block given  
> > from (irb):3:in `select’  
> > from (irb):3

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [January 27, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/inconsistency-map-collect-not-returning-enumerator/182396/3 "2010-01-27T16:30:43Z")

</div>

Gregory B. wrote:

> But this may be to preserve compatibility with Ruby 1.8.6:

Ah, that’s a good enough reason.

‘ri Array#map’ from 1.8.6 doesn’t mention the possibility that it could  
be used without a block, which led me off the scent.

## -------------------------------------------------------------- Array#map array.collect {|item| block } -\> an\_array array.map {|item| block } -\> an\_array

```
 Invokes _block_ once for each element of _self_. Creates a new
 array containing the values returned by the block. See also
 +Enumerable#collect+.

    a = ["a", "b", "c", "d"]
    a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
    a #=> ["a", "b", "c", "d"]
```
