Skip to content

Commit

Permalink
Use loop instead of recursive call for Element#root
Browse files Browse the repository at this point in the history
It's for performance and avoiding stack level too deep.
  • Loading branch information
kou committed Aug 17, 2024
1 parent 790ad5c commit 6422fa3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/rexml/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,14 @@ def root_node
# Related: #root_node, #document.
#
def root
return elements[1] if self.kind_of? Document
return self if parent.kind_of? Document or parent.nil?
return parent.root
target = self
while target
return target.elements[1] if target.kind_of? Document
parent = target.parent
return target if parent.kind_of? Document or parent.nil?
target = parent
end
nil
end

# :call-seq:
Expand Down

0 comments on commit 6422fa3

Please sign in to comment.