module ProgressBar::Refinements::Enumerator

Constants

ARITY_ERROR_MESSAGE

Public Instance Methods

with_progressbar(options = {}) { |*yielded_args| ... } click to toggle source
# File lib/ruby-progressbar/refinements/progress_enumerator.rb, line 8
def with_progressbar(options = {}, &block)
  progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))

  each do |item|
    progress_bar.increment

    next unless block

    yielded_args = []
    yielded_args << item         if block.arity > 0
    yielded_args << progress_bar if block.arity > 1

    fail ::ArgumentError, ARITY_ERROR_MESSAGE if block.arity > 2

    yield(*yielded_args)
  end
end