With the current setup you can't pass any collection to it. You can't change the collection of the enumerator once instantiated.
The current code only works because is block is not instantly executed, therefore you see the error when you try to start iterating (or retrieving items).
enumerator = Enumerator.new(&:map)
enumerator.take(1)
# NoMethodError (undefined method `map' for #<Enumerator::Yielder:0x00000000055b6e90>)
This is because Enumerator::new yields a Enumerator::Yielder which doesn't has the method map.
The above could also be written as:
enumerator = Enumerator.new { |yielder| yielder.map }
If you would like to create an enumerator from a collection the easiest way is to call each without block. Other methods like map also create enumerators without block given.
enumerator = [1, 2, 3].each
#=> #<Enumerator: [1, 2, 3]:each>
If you for some reason still want to create the enumerator by hand it could look like this:
enumerator = Enumerator.new { |yielder| [1, 2, 3].each { |number| yielder << number } }
If the intent was to preselect a method of iterating before you receive the collection, you can do so in the following manner:
# assuming both the collection and block are passed by the user
map = :map.to_proc
result = map.call(collection, &block)
# which is equivalent to
result = collection.map(&block)
each,mapand a lot of others. - 3limin4t0r