-
-
Notifications
You must be signed in to change notification settings - Fork 132
Using Enumerators
A long-requested feature has been to add support for Enumerator
s. For example if you have an array with a bunch of things that you're processing, to create a bar and auto-increment it after every item is processed.
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 require
d by default is not acceptable to me because it then 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.
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.
class MyClass
using ProgressBar::Refinements::Enumerator
def initialize(files = [])
@files = files
end
def process
@files.with_progressbar do |file|
# Do process-y stuff
end
end
end
#!/usr/local/bin/ruby
using ProgressBar::Refinements::Enumerator
@files = files
@files.with_progressbar do |file|
# Do process-y stuff
end