How to create a “clone”-able enumerator for external iteration?
问题 I want to create an enumerator for external iteration via next that is clone -able, so that the clone retains the current enumeration state. As an example, let's say I have a method that returns an enumerator which yields square numbers: def square_numbers return enum_for(__method__) unless block_given? n = d = 1 loop do yield n d += 2 n += d end end square_numbers.take(10) #=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] And I want to enumerate the first 5 square numbers, and for each value, print