Ruby's block iteration is fairly straightforward - it ends cleanly and moves on to the rest of the code.
Iterating with Enumerator, on the other hand, is a bit confusing. If you call: each without a block, an Enumerator is returned instead .: next then you can call it in Enumerator to get each next iterative value.
And then the odd part - when the iteration is completed, instead of a numbering that returns zero, it throws an exception: "The iteration is reached at the end." As a result, it does not even return a value.
For example:
test = [ 'test_value' ] enumerator = test.each enumerator.next >> "test_value" enumerator.next >> StopIteration: iteration reached at end
Is the reason for this just so that nil values ββcan be returned by Enumerator? The answer comes to me only as I publish it (so I'm going to publish it yet), but it seems like it should be.
If so, is this a typical way to solve such problems? It seems strange to use an exception to handle code that essentially executes as expected.
ruby exception enumerator
Asher
source share