Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 297
Jason Avatar asked May 19 '10 05:05

Jason


2 Answers

Most built-in methods that accept a block will return an enumerator in case no block is provided (like String#each_char in your example). For these, there is no reason to use to_enum; both will have the same effect.

A few methods do not return an Enumerator, though. In those case you might need to use to_enum.

# How many elements are equal to their position in the array?
[4, 1, 2, 0].to_enum(:count).each_with_index{|elem, index| elem == index} #=> 2

As another example, Array#product, #uniq and #uniq! didn't use to accept a block. In 1.9.2, this was changed, but to maintain compatibility, the forms without a block can't return an Enumerator. One can still "manually" use to_enum to get an enumerator:

require 'backports/1.9.2/array/product' # or use Ruby 1.9.2+
# to avoid generating a huge intermediary array:
e = many_moves.to_enum(:product, many_responses)
e.any? do |move, response|
  # some criteria
end 

The main use of to_enum is when you are implementing your own iterative method. You typically will have as a first line:

def my_each
  return to_enum :my_each unless block_given?
  # ...
end
like image 177
Marc-André Lafortune Avatar answered Sep 20 '22 08:09

Marc-André Lafortune


I think it has something to do with internal and external Iterators. When you return an enumerator like this:

p = "hello".enum_for(:each_char)

p is an external enumerator. One advantage of external iterators is that:

External iterators are more flexible than internal iterators. It's easy to compare two collections for equality with an external iterator, for example, but it's practically impossible with internal iterators…. But on the other hand, internal iterators are easier to use, because they define the iteration logic for you. [From The Ruby Programming Language book, ch. 5.3]

So, with external iterator you can do, e.g.:

p = "hello".enum_for(:each_char)
loop do
    puts p.next
end
like image 31
Marcin Avatar answered Sep 23 '22 08:09

Marcin