Created
June 14, 2017 18:14
-
-
Save wconrad/b3c3753f0c262c1d4dcb5547199d6ff6 to your computer and use it in GitHub Desktop.
Monkey-patch enumerator to call a progress object when iterating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
"increment" | |
2 | |
"increment" | |
3 | |
"increment" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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