Skip to content

Commit

Permalink
[support] Use loop-based DFS to resolve rspec/rspec-support#341 (Syst…
Browse files Browse the repository at this point in the history
…emStackError)

This commit was imported from rspec/rspec-support@b5be67a.
  • Loading branch information
craigjbass authored and myronmarston committed Jan 30, 2018
1 parent bc0be61 commit 14b5c3e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rspec-support/lib/rspec/support/source/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ def location
@location ||= args.find { |arg| arg.is_a?(Location) }
end

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

yield self
node_queue = []
node_queue << self

children.each do |child|
child.each(&block)
while (current_node = node_queue.shift)
yield current_node
node_queue.concat(current_node.children)
end
end

Expand Down

0 comments on commit 14b5c3e

Please sign in to comment.