What is the advantage of creating an enumerable object using to_enum in Ruby?

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-05-19T05:21:03Z Indexed on 2010/05/19 5:30 UTC
Read the original article Hit count: 168

Filed under:
|

Hi,

Why would you create a proxy reference to an object in Ruby, by using the to_enum method rather than just using the object directly? I cannot think of any practical use for this, trying to understand this concept & where someone might use it, but all the examples I have seen seem very trivial.

For example, why use:

"hello".enum_for(:each_char).map {|c| c.succ }

instead of

"hello".each_char.map {|c| c.succ }

I know this is a very simple example, does anyone have any real-world examples?

Thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about enumeration