Skip to content

Instantly share code, notes, and snippets.

@kotay
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotay/8859883 to your computer and use it in GitHub Desktop.
Save kotay/8859883 to your computer and use it in GitHub Desktop.
Progress
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
@kotay
Copy link
Author

kotay commented Feb 7, 2014

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment