From: Robert Klemme Date: 2006-07-08T19:50:05+09:00 Subject: Re: libhxml Node#remove! kills each loop and extension to #<< 2006/7/8, transfire@gmail.com : > 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.each { |node| > if XML::Node === node > node.content = "yep" > node.remove! if node['id'] = "b" > end > } > > The result is > > > yep > > > > Would tha tbe a bug? Or something that simply can't be avoided? It's usually a problem to change a container while iterating through it. This can generate all sorts of weird effects. It's generally better to rely on this *not* being possible unless explicitely stated (e.g most of Java's iterators implement remove() which savely removes an element while iterating). In your case I'd either first remove the one you want to get rid of, iterate using an index (if that's possible) or remember objects to remove in some kind of container and do the removal after the iteration (probably the most efficient solution). Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/