does Object#to_enum exist?

D

Dan Zwell

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:
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
 
S

Stefano Crocco

Alle gioved=EC 26 aprile 2007, Dan Zwell ha scritto:
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:

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

You need to require 'enumerator'. Doing this will result in the to_enum met=
hod=20
to be added to the Kernel module and to class Object, which mixes Kernel in=
=2E=20
=46or example

require 'enumerator'

"abcdef".to_enum:)each_byte).each{|b| puts b.chr}
a
b
c
d
e
f

I hope this helps

Stefano
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top