Skip to content

Using Enumerators

Jeff Felchner edited this page Sep 15, 2017 · 8 revisions

A long-requested feature has been to add support for Enumerators. For example if you have an array with a bunch of things that you're processing, people wanted a way to automatically create a bar and auto-increment it after every item is processed.

Why Wasn't Previously Implemented

The reason I've been resistant in the past is because monkey-patching core Ruby classes is not something that I'm willing to do. Even including a file which is not required by default is not acceptable to me because once it's manually required, it still steps on the toes of everyone else's libraries.

Fortunately since Ruby 2.0, we have another option. Refinements. I'm not a huge fan of these either, but that is a different story. For this particular ruby-progressbar use case however, it quashes all of my qualms that I outlined above and gives my users something that they've been asking for.

How To Use It

If you want to add Enumerator support to your project, all you have to do is include the refinement in whatever class or script you want to use it in.

Once you've done that, you can use with_progressbar on any Enumerator object.

Customizing the Enumerated Bar

You can pass a hash to with_progressbar. It accepts any options that ProgressBar.create accepts. The only option you cannot set is (obviously) total and starting_at, since you must start at 0 and total comes from the size of the Enumerator you're calling with_progressbar on.

Example

Note: this is not meant to be an efficient implementation of Fibonacci. 😉

class Fibonacci
  using ProgressBar::Refinements::Enumerator

  def self.generate(quantity = 10)
    sequence = []

    quantity.times.with_progressbar(progress_mark: '+') do |index|
      sequence << fibonacci(index)
    end

    sequence
  end

  private

  def self.fibonacci(position)
    return position if ( 0..1 ).include?(position)

    fibonacci(position - 1) + fibonacci(position - 2)
  end
end

Output

This is what the output of calling the above script would produce (as with the other examples, I added sleep in so that you can actually see the progress happening):

enumerator-fibonacci-generation