T
transfire
hi,
While using #each to loop thru the children of a Node, if I remove a
node the loop breaks on it's own.
<root>
<a id="a"></a>
<b id="b"></b>
<c id="c"></c>
</root>
root.each { |node|
if XML::Node === node
node.content = "yep"
node.remove! if node['id'] = "b"
end
}
The result is
<root>
<a id="a">yep</a>
<c id="c"></c>
</root>
Would tha tbe a bug? Or something that simply can't be avoided?
Also, I found this extension to #<< to be useful:
class XML::Node
alias_method :append, :<<
def <<( node )
if Array === node
node.each { |n| self.append n }
else
super
end
end
end
Thanks,
T.
While using #each to loop thru the children of a Node, if I remove a
node the loop breaks on it's own.
<root>
<a id="a"></a>
<b id="b"></b>
<c id="c"></c>
</root>
root.each { |node|
if XML::Node === node
node.content = "yep"
node.remove! if node['id'] = "b"
end
}
The result is
<root>
<a id="a">yep</a>
<c id="c"></c>
</root>
Would tha tbe a bug? Or something that simply can't be avoided?
Also, I found this extension to #<< to be useful:
class XML::Node
alias_method :append, :<<
def <<( node )
if Array === node
node.each { |n| self.append n }
else
super
end
end
end
Thanks,
T.