Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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, dependencies
Breakfast, [cook, make coffee]
need some rails power? www.tinci.fr@happynoff
todo = TaskList.new
todo.add 'breakfast', ['make coffee', 'cook']
todo.add '...
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, de...
need some rails power? www.tinci.fr@happynoff
class TaskList
# ...
def tsort_each_node(&block)
@tasks.each_key(&block)
end...
need some rails power? www.tinci.fr@happynoff
class TaskList
# ...
def tsort_each_child(node, &block)
@tasks[node].each(&b...
need some rails power? www.tinci.fr@happynoff
todo = TaskList.new
todo.add 'breakfast', ['make coffee', 'cook']
todo.add '...
need some rails power? www.tinci.fr@happynoff
Questions?
need some rails power? www.tinci.fr@happynoff
Thanks!
Upcoming SlideShare
Loading in …5
×

Dependency sorting in Ruby with TSort

827 views

Published on

Slides of the talk I gave at ParisRB on 03/03/15

Published in: Software
  • Be the first to comment

  • Be the first to like this

Dependency sorting in Ruby with TSort

  1. 1. need some rails power? www.tinci.fr@happynoff Dependency sorting in Ruby with TSort
  2. 2. need some rails power? www.tinci.fr@happynoff Simon Courtois www.tinci.fr happynoff simonc
  3. 3. need some rails power? www.tinci.fr@happynoff Breakfast Make coffee Cook Buy eggs Buy stuff Buy coffee
  4. 4. need some rails power? www.tinci.fr@happynoff Topological Sort
  5. 5. need some rails power? www.tinci.fr@happynoff TSORT
  6. 6. need some rails power? www.tinci.fr@happynoff require 'tsort' It's in the Ruby's Standard Library!
  7. 7. need some rails power? www.tinci.fr@happynoff Task: name, dependencies Breakfast, [cook, make coffee]
  8. 8. 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'
  9. 9. need some rails power? www.tinci.fr@happynoff class TaskList include TSort end
  10. 10. need some rails power? www.tinci.fr@happynoff class TaskList include TSort def initialize @tasks = {} end end
  11. 11. 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
  12. 12. need some rails power? www.tinci.fr@happynoff class TaskList # ... def tsort_each_node(&block) @tasks.each_key(&block) end end
  13. 13. need some rails power? www.tinci.fr@happynoff class TaskList # ... def tsort_each_child(node, &block) @tasks[node].each(&block) end end
  14. 14. 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
  15. 15. need some rails power? www.tinci.fr@happynoff Questions?
  16. 16. need some rails power? www.tinci.fr@happynoff Thanks!

×