Last active
August 29, 2015 13:56
-
-
Save kotay/8859883 to your computer and use it in GitHub Desktop.
Progress
This file contains 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 initialize(msg, total, &context) | |
@total = total | |
@context = context | |
end | |
def update(progress) | |
puts(progress) | |
end | |
def fail(msg = "") | |
puts "failed" | |
# How do I exit from the block? | |
end | |
end | |
## Progress DSL | |
def task(msg, total=100, &block) | |
yield(Progress.new(msg, total, &block)) | |
end | |
# Works nicely | |
task("--> starting") do |progress| | |
(1..50).to_a.each do |i| | |
sleep 0.01 | |
progress.update(i) | |
end | |
end | |
# I want to exit from the outer block | |
task("--> starting") do |progress| | |
(1..50).to_a.each do |i| | |
sleep 0.01 | |
progress.fail("Woops") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want the
progress.fail
to exit early from the block, at the moment it blindly continues. Do I need to pass the context to the Progress class:Progress.new(msg, total, &block)
and somehow send the context a break/return?