What is the correct way to check if the Iterator is complete?

As indicated in the header, I would like to know how to correctly check if the iterator is complete.

I did not find anything in the docs, but found something like this in the source:

iter.next.is_a? Iterator::Stop 

Toy example:

 a = "aδΈ–c" b = a.each_char puts b.next # a puts b.next # δΈ–puts b.next # c puts b.next # #<Iterator::Stop:0x8ccbff8> if b.next.is_a? Iterator::Stop puts "DONE" # successfully prints DONE end 

This is right and right, or should I use a different way.

+5
source share
1 answer

Yes this is correct. But most of the time, when you do not need to deal with next , you just bind the iterators and get the result from them.

+3
source

Source: https://habr.com/ru/post/1232758/


All Articles