Method: TTY::ProgressBar#iterate
- Defined in:
- lib/tty/progressbar.rb
#iterate(collection, progress = 1, &block) ⇒ Enumerator
Note:
If total is set, iteration will NOT stop after this number of iterations, only when provided Enumerable is finished. It may be convenient in “unsure number of iterations” situations (like downloading in chunks, when server may eventually send more chunks than predicted), but be careful to not pass infinite enumerators without previously doing ‘.take(some_finite_number)` on them.
Iterate over collection either yielding computation to block or provided Enumerator. If the bar’s total was not set, it would be taken from collection.count, otherwise previously set total would be used. This allows using the progressbar with infinite, lazy, or slowly-calculated enumerators.
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/tty/progressbar.rb', line 259 def iterate(collection, progress = 1, &block) update(total: collection.count * progress) unless total progress_enum = Enumerator.new do |iter| collection.each do |elem| advance(progress) iter.yield(elem) end end block_given? ? progress_enum.each(&block) : progress_enum end |