Skip to content

Instantly share code, notes, and snippets.

@adamsanderson
Created December 22, 2010 19:45
Show Gist options
  • Save adamsanderson/751991 to your computer and use it in GitHub Desktop.
Save adamsanderson/751991 to your computer and use it in GitHub Desktop.
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