Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > does Object#to_enum exist?

Reply
Thread Tools

does Object#to_enum exist?

 
 
Dan Zwell
Guest
Posts: n/a
 
      04-26-2007
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

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      04-26-2007
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:
> >> "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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET 2.0 ASPx Page does not load, but HTM does prabhupr@hotmail.com ASP .Net 1 02-08-2006 12:57 PM
Button OnClick does not fire on first postback, but does on second Janet Collins ASP .Net 0 01-13-2006 10:08 PM
Does the 2.0 Framework come out when Visual Studio .NET 2005 does? needin4mation@gmail.com ASP .Net 3 10-07-2005 12:55 AM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments