jemnotesversion 2 / featuring this entry or see all/search

Aug 5
From the pickaxe book.
Create an enumerator object: e = (1..10).to_enum, or e = (1..10).each (without a block). Then, x = e.next. to_enum and enum_for are the same.
Or, create multiple, and use a loop to figure out the details (and throw a StopIteration when any of the iterators run out of elements).
x = ('a'..'z').to_enum
y = (1..10).to_enum
loop do
  puts "#{x.next}: #{y.next}"
end
Can also use with_index on an enumerator to convert it back for use with a code block. Or call a method for each element of the iterator, then convert it to an array: (1..10).to_enum(:each_slice, 3).to_a.