Method: TSort#tsort_each
- Defined in:
- lib/tsort.rb
#tsort_each(&block) ⇒ Object
The iterator version of the #tsort method. <em>obj</em>.tsort_each is similar to <em>obj</em>.tsort.each, but modification of obj during the iteration may lead to unexpected results.
#tsort_each returns nil. If there is a cycle, TSort::Cyclic is raised.
class G
include TSort
def initialize(g)
@g = g
end
def tsort_each_child(n, &b) @g[n].each(&b) end
def tsort_each_node(&b) @g.each_key(&b) end
end
graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
graph.tsort_each {|n| p n }
#=> 4
# 2
# 3
# 1
202 203 204 205 206 |
# File 'lib/tsort.rb', line 202 def tsort_each(&block) # :yields: node each_node = method(:tsort_each_node) each_child = method(:tsort_each_child) TSort.tsort_each(each_node, each_child, &block) end |