Skip to content

Instantly share code, notes, and snippets.

@bachue
Last active December 15, 2015 02:38
Show Gist options
  • Save bachue/5188255 to your computer and use it in GitHub Desktop.
Save bachue/5188255 to your computer and use it in GitHub Desktop.
My TSort Example
require 'tsort'
require 'pp'
Node = Struct.new :value, :before, :after
class Collection < Array
include TSort
alias :tsort_each_node :each
def tsort_each_child(current, &block)
select { |competitor|
current.after == competitor.value || # Is there any node be run after the current node?
competitor.before == current.value # Is there any node run before the current node?
}.each(&block)
end
end
collection = Collection.new
collection << Node.new(1, 2, 4)
collection << Node.new(2)
collection << Node.new(3, 4)
collection << Node.new(4, 1, 3)
pp collection.strongly_connected_components
# [[#<struct Node value=3, before=4, after=nil>],
# [#<struct Node value=4, before=1, after=3>],
# [#<struct Node value=1, before=2, after=4>],
# [#<struct Node value=2, before=nil, after=nil>]]
@bachue
Copy link
Author

bachue commented Mar 18, 2013

This code is to test initializer tsort in rails 3 initialization process

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment