You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. 😉
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):