Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created June 14, 2017 18:14
Show Gist options
  • Save wconrad/b3c3753f0c262c1d4dcb5547199d6ff6 to your computer and use it in GitHub Desktop.
Save wconrad/b3c3753f0c262c1d4dcb5547199d6ff6 to your computer and use it in GitHub Desktop.
Monkey-patch enumerator to call a progress object when iterating
1
"increment"
2
"increment"
3
"increment"
class Progress
def increment
p 'increment'
end
end
module Enumerable
def with_progress(progress)
Enumerator.new do |yielder|
each do |e|
yielder.yield e
progress.increment
end
end
end
end
progress = Progress.new
[1, 2, 3].with_progress(progress).each do |e|
p e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment