dependency sorting in ruby with tsort
TRANSCRIPT
need some rails power? www.tinci.fr@happynoff
Dependency sorting in Ruby with TSort
need some rails power? www.tinci.fr@happynoff
Simon Courtois
www.tinci.fr
happynoff
simonc
need some rails power? www.tinci.fr@happynoff
Breakfast
Make coffee
Cook Buy eggs
Buy stuff
Buy coffee
need some rails power? www.tinci.fr@happynoff
Topological Sort
need some rails power? www.tinci.fr@happynoff
TSORT
need some rails power? www.tinci.fr@happynoff
require 'tsort'It's in the Ruby's Standard Library!
need some rails power? www.tinci.fr@happynoff
Task: name, dependenciesBreakfast, [cook, make coffee]
need some rails power? www.tinci.fr@happynoff
todo = TaskList.new todo.add 'breakfast', ['make coffee', 'cook'] todo.add 'cook', ['buy stuff'] todo.add 'make coffee', ['buy stuff'] todo.add 'buy stuff', ['buy coffee', 'buy eggs'] todo.add 'buy coffee' todo.add 'buy eggs'
need some rails power? www.tinci.fr@happynoff
class TaskList include TSort end
need some rails power? www.tinci.fr@happynoff
class TaskList include TSort
def initialize @tasks = {} end end
need some rails power? www.tinci.fr@happynoff
class TaskList include TSort
def initialize @tasks = {} end
def add(name, dependencies = []) @tasks[name] = dependencies end end
need some rails power? www.tinci.fr@happynoff
class TaskList # ...
def tsort_each_node(&block) @tasks.each_key(&block) end end
need some rails power? www.tinci.fr@happynoff
class TaskList # ...
def tsort_each_child(node, &block) @tasks[node].each(&block) end end
need some rails power? www.tinci.fr@happynoff
todo = TaskList.new todo.add 'breakfast', ['make coffee', 'cook'] todo.add 'cook', ['buy stuff'] todo.add 'make coffee', ['buy stuff'] todo.add 'buy stuff', ['buy coffee', 'buy eggs'] todo.add 'buy coffee' todo.add 'buy eggs'
puts todo.tsort # >> buy coffee # >> buy eggs # >> buy stuff # >> make coffee # >> cook # >> breakfast
need some rails power? www.tinci.fr@happynoff
Questions?
need some rails power? www.tinci.fr@happynoff
Thanks!