Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 492e223

Browse files
authored
Merge pull request #343 from craigjbass/use-loop-based-depth-first-search
Use loop-based DFS to resolve #341 (SystemStackError)
2 parents 2f9091b + bbb2c88 commit 492e223

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/rspec/support/source/node.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ def location
4545
@location ||= args.find { |arg| arg.is_a?(Location) }
4646
end
4747

48-
def each(&block)
48+
# We use a loop here (instead of recursion) to prevent SystemStackError
49+
def each
4950
return to_enum(__method__) unless block_given?
5051

51-
yield self
52+
node_queue = []
53+
node_queue << self
5254

53-
children.each do |child|
54-
child.each(&block)
55+
while (current_node = node_queue.shift)
56+
yield current_node
57+
node_queue.concat(current_node.children)
5558
end
5659
end
5760

0 commit comments

Comments
 (0)