0
votes

I've read about Enumerator class. And I can't fully understand what's going on here:

[0, 1].each { |i| puts i }

By itself [0, 1].each returns => #<Enumerator: [0, 1]:each>

So the question is Are we pathing block to each method of the array or to the instance of the Enumerator class?

2

2 Answers

3
votes

As many other methods, Array#each returns and Enumerator if a block is not passed but it iterates over the array and calls the block for each item if a block is passed.

The values returned by the block for each array item are the elements of the array returned by Array#each when a block is passed.

To answer your question, the block and the Enumerator never met.

0
votes

You can't pass arguments (including blocks) to objects. The only things that can take arguments (including blocks) are message sends and blocks. So, obviously, it has to be the former.