Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created April 10, 2019 03:55
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 stephancom/40cf6c18931f6719f02f5cd469119559 to your computer and use it in GitHub Desktop.
Save stephancom/40cf6c18931f6719f02f5cd469119559 to your computer and use it in GitHub Desktop.
class BlockQueue
def initialize(name)
@name = name
@blocks = []
end
def run_later(&block)
Progress.log "queueing job for #{@name}"
@blocks << block
end
def run_each
Progress.phase = @name
Progress.log "running #{@blocks.count} jobs from #{@name}"
@blocks.each do |block|
yield block
end
end
end
#!/usr/bin/env ruby
# .----------------. .----------------. .----------------. .----------------. .----------------. .----------------.
# | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
# | | ______ | || | _____ | || | ____ | || | ______ | || | ___ ____ | || | ____ ____ | |
# | | |_ _ \ | || | |_ _| | || | .' `. | || | .' ___ | | || | |_ ||_ _| | || | |_ _||_ _| | |
# | | | |_) | | || | | | | || | / .--. \ | || | / .' \_| | || | | |_/ / | || | \ \ / / | |
# | | | __'. | || | | | _ | || | | | | | | || | | | | || | | __'. | || | > `' < | |
# | | _| |__) | | || | _| |__/ | | || | \ `--' / | || | \ `.___.'\ | || | _| | \ \_ | || | _/ /'`\ \_ | |
# | | |_______/ | || | |________| | || | `.____.' | || | `._____.' | || | |____||____| | || | |____||____| | |
# | | | || | | || | | || | | || | | || | | |
# | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
# '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
#
# a block experiment
class Blockx
def initialize(targets)
@targets = targets
@blocks = Hash[@targets.map { |t| [t, []] }]
end
def run_later(target, name, &block)
puts "Will run #{name} on #{target} later"
@blocks[target] << block
end
def run_all
@blocks.each_pair do |target, blocks|
puts "About to run #{blocks.count} blocks on #{target}"
blocks.each do |block|
block.call(target)
end
puts "completed #{target}"
end
end
end
bb = Blockx.new(%w[a b c d e])
%w[a b c].each do |t|
bb.run_later(t, 'first') do |target|
puts "I am running the first thing on #{target}"
end
end
%w[e c a].each do |t|
bb.run_later(t, 'second') do |target|
puts "I am running the second thing on #{target}"
end
end
%w[d b c].each do |t|
bb.run_later(t, 'third') do |target|
puts "I am running the second thing on #{target}"
end
end
def fourth(target)
puts "This was a method, which I'm doing fourth to #{target}"
end
%w[b c d e].each do |t|
bb.run_later(t, 'fourth', &method(:fourth))
end
%w[a b c d e].each do |t|
bb.run_later(t, 'final') do |target|
puts "I am running the final thing on #{target}"
end
end
puts "starting run all"
bb.run_all
puts "complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment