Created
December 22, 2010 19:45
-
-
Save adamsanderson/751991 to your computer and use it in GitHub Desktop.
An example of using TSort
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
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