Module: Enumerable

Defined in:
lib/with_progress/extensions/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#with_progress(options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/with_progress/extensions/enumerable.rb', line 2

def with_progress(options = {})
  return to_enum(:with_progress, options) { size rescue nil } unless block_given?

  options = WithProgress::DEFAULTS.merge(options)
  unless options.has_key?(:total)
    options[:total] = size rescue nil
  end

  bar = ProgressBar.create(options)
  each do |*args|
    result = yield *args
    bar.increment
    result
  end
ensure
  bar.stop if bar
end