class Hpricot::Elements

def add_class class_name

Now all paragraphs will have class="bacon".

(doc/"p").add_class("bacon")

Adds the class to all matched elements.
def add_class class_name
  each do |el|
    next unless el.respond_to? :get_attribute
    classes = el.get_attribute('class').to_s.split(" ")
    el.set_attribute('class', classes.push(class_name).uniq.join(" "))
  end
  self
end