Modifying attributes with Hpricot

If you’re processing HTML with Hpricot and want to change the
attributes on a tag in a search block, beware that the attributes
method on an element returns a copy of the actual attributes. In
order to change the real attribute on the element, you need to set
raw_attributes on it. Here’s what I had to do, boiled down to the
scum on the pan:

doc=Hpricot(html)doc.search("img").eachdo|element|# do some processing here, calculating the new attribute value# ...element.raw_attributes=element.attributes.merge("src"=>new_src)end# now save the tweaked documentfile.write(doc.to_html)