# Enumerator of what?

**URL:** <https://www.ruby-forum.com/t/enumerator-of-what/172283>\
**Category:** Ruby\
**Created:** [July 18, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/enumerator-of-what/172283 "2009-07-18T16:52:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [July 18, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/enumerator-of-what/172283/1 "2009-07-18T16:52:26Z")

</div>

Any way to ask an Enumerator what the underlying object it is  
enumerating for is? Eg.

e = Enumerator.new([1,2,3], :each)  
e.some\_method #=\> [1,2,3]

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [July 18, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/enumerator-of-what/172283/2 "2009-07-18T18:10:44Z")

</div>

Not as far as I know,

but this patch could do it if you really need it

— enumerator.c 2009-04-19 15:33:31.000000000 +0200  
+++ enumerator-with-obj-access.c 2009-07-18 18:02:33.000000000  
+0200  
@@ -537,6 +537,12 @@  
return v;  
}

+static VALUE  
+enumerator\_get\_object( VALUE obj )  
+{

- 

```
  return enumerator_ptr( obj )->obj;

```

+}  
+  
/\*

- call-seq:
- e.rewind =\> e  
@@ -788,6 +794,7 @@  
rb\_define\_method(rb\_cEnumerator, “initialize”,  
enumerator\_initialize, -1);  
rb\_define\_method(rb\_cEnumerator, “initialize\_copy”,  
enumerator\_init\_copy, 1);  
rb\_define\_method(rb\_cEnumerator, “each”, enumerator\_each, 0);

- rb\_define\_method(rb\_cEnumerator, “get\_object”,  
enumerator\_get\_object, 0);  
rb\_define\_method(rb\_cEnumerator, “each\_with\_index”,  
enumerator\_with\_index, 0);  
rb\_define\_method(rb\_cEnumerator, “each\_with\_object”,  
enumerator\_with\_object, 1);  
rb\_define\_method(rb\_cEnumerator, “with\_index”,  
enumerator\_with\_index, 0);

please note that  
2.times.get\_object will give you 2 and not [0, 1] which 2.times.to\_a  
would give you.

Cheers  
Robert

On 7/18/09, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any way to ask an Enumerator what the underlying object it is  
> enumerating for is? Eg.
> 
> e = Enumerator.new([1,2,3], :each)  
> e.some\_method #=\> [1,2,3]

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [July 18, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/enumerator-of-what/172283/3 "2009-07-18T21:28:56Z")

</div>

> but this patch could do it if you really need it

Is this functionality…useful enough to merit being submitted to core?

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [July 18, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/enumerator-of-what/172283/4 "2009-07-18T23:05:17Z")

</div>

On 7/18/09, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > but this patch could do it if you really need it
> 
> Is this functionality…useful enough to merit being submitted to core?  
> I do not think so, I was in a strange mood and as there is no other  
> way…  
> I suspect that enumerator hides its obj by designed.

But of course this patch is in the public domain now.

Cheers  
Robert

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
