Forum: Ruby How to delete a node with Hpricot?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-05-21 14:04
(Received via mailing list)

Sorry if this is not the right forum for this question.

If a node is bad I'm trying to comment it out.   If it's really bad
I'm trynig to delete it.

The way I'm trying to do is is as follows.

def this_is_a_problem
  doc = Hpricot( html )
  doc.traverse_element do |node|
  if some_bad_node_test
    unless really_bad?
     node.swap( "<!-- comment out node #{node.to_html} -->" )
       node.swap( "" )

However I'm getting a nasty error.

TypeError: no implicit conversion from nil to integer

Am I doing this the wrong way?


8bc543795b502900b5333aea73ad5533?d=identicon&s=25 Eden Li (edenli)
on 2007-05-22 04:36
(Received via mailing list)
I can't comment on the error, but you can delete a given node in your
traverse_element block by doing node.parent.children.delete(node).

Alternatively, you can run a search first to remove the "really bad
elements."  If you define really bad elements in terms of an xpath
query, this becomes very simple:


Then you can go through and swap things out as necessary:

 (doc/"xpath to bad nodes").each do |el|
  el.inner_html = "<!-- #{el.to_html} -->"
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-05-22 05:10
(Received via mailing list)
On 5/22/07, eden li <> wrote:
> > I'm trynig to delete it.
> >        node.swap( "" )
> >
> > Am I doing this the wrong way?
> >
> > Thanx
> >
> > Daniel

Sweet, that looks like a good way to go.  I'll try that out.

I've actually got it working but it's very very slow, hopefully this
will speed it up

This topic is locked and can not be replied to.