检查迭代器是否完整的正确方法是什么?
What is the proper way to check if an Iterator is complete?
如标题所述,我想知道检查迭代器是否完整的正确方法。
我在文档中找不到任何内容,但我在 source 中找到了类似的内容:
iter.next.is_a? Iterator::Stop
玩具示例:
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
这是正确和正确的还是我应该使用不同的方式。
是的,这是正确的方法。但大多数时候你不需要处理 next
,你只需链接迭代器并从中得到结果。
如标题所述,我想知道检查迭代器是否完整的正确方法。
我在文档中找不到任何内容,但我在 source 中找到了类似的内容:
iter.next.is_a? Iterator::Stop
玩具示例:
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
这是正确和正确的还是我应该使用不同的方式。
是的,这是正确的方法。但大多数时候你不需要处理 next
,你只需链接迭代器并从中得到结果。