An example of using TSort
require 'tsort' | |
class JobRunner | |
include TSort | |
Job = Struct.new(:name, :dependencies) | |
def initialize() | |
@jobs = Hash.new{|h,k| h[k] = []} | |
end | |
alias_method :execute, :tsort | |
def add(name, dependencies=[]) | |
@jobs[name] = dependencies | |
end | |
def tsort_each_node(&block) | |
@jobs.each_key(&block) | |
end | |
def tsort_each_child(node, &block) | |
@jobs[node].each(&block) | |
end | |
end | |
if __FILE__ == $0 | |
runner = JobRunner.new | |
runner.add('breakfast', ['serve']) | |
runner.add('serve', ['cook']) | |
runner.add('cook', ['buy eggs','buy bacon']) | |
puts runner.execute | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment