# Question about magic enumerators in \> 1.8

**URL:** <https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677>\
**Category:** Ruby\
**Created:** [July 9, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677 "2006-07-09T16:06:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 9, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/1 "2006-07-09T16:06:04Z")

</div>

Hi –

Can someone jog my memory, and/or enlighten me, as to what a case  
would be where it’s useful to have:

array.map

return an Enumerator? I can’t seem to come up with an example where:

array.map.anything

is different from

array.anything

Thanks –

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 9, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/2 "2006-07-09T16:34:45Z")

</div>

On Jul 9, 2006, at 9:03 AM, removed\_email\_address@domain.invalid wrote:

> is different from
> 
> array.anything
> 
> Thanks –

Well, you can use it to roll the oft-requested map\_with\_index():

array.each\_with\_index.map { |obj, i| … }

That’s not a map() example though, so maybe not what you wanted.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 9, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/3 "2006-07-09T16:38:46Z")

</div>

Hi –

On Sun, 9 Jul 2006, James Edward G. II wrote:

> > 
> 
> array.each\_with\_index.map { |obj, i| … }
> 
> That’s not a map() example though, so maybe not what you wanted.

Right, it isn’t 🙂 I’m still looking for a map example.

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 10, 2006, 4:10am UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/4 "2006-07-10T04:10:35Z")

</div>

On Jul 9, 2006, at 10:36 AM, removed\_email\_address@domain.invalid wrote:

> > > array.map
> 
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> [Ruby for Rails](http://www.manning.com/black) =\> RUBY FOR RAILS, the Ruby book for  
> Rails developers  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
> removed\_email\_address@domain.invalid =\> me

Contrived example: I know a method I’m about to call iterates thru me  
with each doing something. It’s the last thing it does and what the  
results of the thing it does rather than myself at the end:

some\_method(array.map)

Ok so it’s a REALLY contrived example.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 10, 2006, 11:37am UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/5 "2006-07-10T11:37:24Z")

</div>

> > > > > “d” == dblack [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

d\> return an Enumerator? I can’t seem to come up with an example where:

d\> array.map.anything

d\> is different from

d\> array.anything

moulon% ./ruby -e ‘[12].map.with\_index {}’  
moulon%

moulon% ./ruby -e ‘[12].with\_index {}’  
-e:1: undefined method `with\_index’ for [12]:Array (NoMethodError)  
moulon%

p.s.:  
:-))))

–

Guy Decoux

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 10, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/6 "2006-07-10T18:53:09Z")

</div>

Hi –

On Mon, 10 Jul 2006, Christian N. wrote:

> > array.map.anything
> > 
> > is different from
> > 
> > array.anything
> 
> names.map { |x| x.size }.max, and you don’t need an immediate array.

That’s not an example of what I’m asking about, though. There’s no  
enumerator returned there.

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 10, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/question-about-magic-enumerators-in-1-8/97677/7 "2006-07-10T13:07:00Z")

</div>

removed\_email\_address@domain.invalid writes:

> is different from
> 
> array.anything

names.map { |x| x.size }.max, and you don’t need an immediate array.
