# Does Object#to\_enum exist?

**URL:** <https://www.ruby-forum.com/t/does-object-to-enum-exist/92982>\
**Category:** Ruby\
**Created:** [April 26, 2007, 9:47am UTC](https://www.ruby-forum.com/t/does-object-to-enum-exist/92982 "2007-04-26T09:47:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaj135](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@cmaj135](https://www.ruby-forum.com/u/cmaj135)\
**Post date:** [April 26, 2007, 9:47am UTC](https://www.ruby-forum.com/t/does-object-to-enum-exist/92982/1 "2007-04-26T09:47:17Z")

</div>

Hi, all.

I saw someone post a snippet of code that turned an object (with some  
sort of “each” method) into an enumerable. It looked really neat, until  
I tried:

> > “abcdef”.to\_enum(:each\_byte)  
> > NoMethodError: undefined method `to\_enum’ for “abcdef”:String  
> > from (irb):1
> 
> >

I looked it up and ri seems to think I have Object#to\_enum. The  
underlying code is c, so frankly, I don’t understand it. Even running  
the examples from the documentation or looking at Object.new.methods, I  
see nothing about this elusive method. Does anybody have any insight?  
(I’m using ruby 1.8.6.)

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![cmaj135](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@cmaj135](https://www.ruby-forum.com/u/cmaj135)\
**Post date:** [April 26, 2007, 10:09am UTC](https://www.ruby-forum.com/t/does-object-to-enum-exist/92982/2 "2007-04-26T10:09:22Z")

</div>

Hi,

In message “Re: does Object#to\_enum exist?”  
on Thu, 26 Apr 2007 16:45:36 +0900, Dan Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|I saw someone post a snippet of code that turned an object (with some  
|sort of “each” method) into an enumerable. It looked really neat, until  
|I tried:  
|  
| \>\> “abcdef”.to\_enum(:each\_byte)  
|NoMethodError: undefined method `to\_enum’ for “abcdef”:String  
| from (irb):1  
| \>\>  
|  
|I looked it up and ri seems to think I have Object#to\_enum.

It’s available only for 1.9. You can use it with Enumerator  
additional library installed.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![cmaj135](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@cmaj135](https://www.ruby-forum.com/u/cmaj135)\
**Post date:** [April 26, 2007, 10:14am UTC](https://www.ruby-forum.com/t/does-object-to-enum-exist/92982/3 "2007-04-26T10:14:23Z")

</div>

Alle giovedì 26 aprile 2007, Dan Z. ha scritto:

> I looked it up and ri seems to think I have Object#to\_enum. The  
> underlying code is c, so frankly, I don’t understand it. Even running  
> the examples from the documentation or looking at Object.new.methods, I  
> see nothing about this elusive method. Does anybody have any insight?  
> (I’m using ruby 1.8.6.)
> 
> Thanks,  
> Dan

You need to require ‘enumerator’. Doing this will result in the to\_enum  
method  
to be added to the Kernel module and to class Object, which mixes Kernel  
in.  
For example

require ‘enumerator’

“abcdef”.to\_enum(:each\_byte).each{|b| puts b.chr}  
a  
b  
c  
d  
e  
f

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![cmaj135](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@cmaj135](https://www.ruby-forum.com/u/cmaj135)\
**Post date:** [April 26, 2007, 10:24am UTC](https://www.ruby-forum.com/t/does-object-to-enum-exist/92982/4 "2007-04-26T10:24:23Z")

</div>

Thank you. I feel silly for making such a newbie-ish mistake, but then  
again, I’m a newbie.

Dan

---

<div class="post-metadata">

**Author:** ![cmaj135](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8edcca/32.png) [@cmaj135](https://www.ruby-forum.com/u/cmaj135)\
**Post date:** [April 26, 2007, 10:42am UTC](https://www.ruby-forum.com/t/does-object-to-enum-exist/92982/5 "2007-04-26T10:42:26Z")

</div>

On 4/26/07, Dan Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you. I feel silly for making such a newbie-ish mistake, but then  
> again, I’m a newbie.  
> It is from our mistakes that we learn, I was about to check for  
> #to\_enum very recently but was too lazy and/or proud to ask, so I  
> learned too 🙂

Cheers  
Robert
