hpricot:An Hpricot Showcase:Hpricot Basics:Looping Edits

>> require "hpricot"
=> true
>> doc = Hpricot("<html><body><p class='posted'>A simple <b>test</b> string.</p><p>foo</p></body></html>")
=> #<Hpricot::Doc {elem <html> {elem <body> {elem <p class="posted"> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p> "foo" </p>} </body>} </html>}>
>> (doc/"p.posted").each {|x| x.set_attribute :class, 'newLinks'}
=> #<Hpricot::Elements[{elem <p class="newLinks"> "A simple " {elem <b> "test" </b>} " string." </p>}]>
>> doc
=> #<Hpricot::Doc {elem <html> {elem <body> {elem <p class="newLinks"> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p> "foo" </p>} </body>} </html>}>
>> doc.to_s
=> "<html><body><p class=\"newLinks\">A simple <b>test</b> string.</p><p>foo</p></body></html>"
>> doc = Hpricot("<html><body><p class='posted'>A simple <b>test</b> string.</p><p class='posted'>foo</p></body></html>")
=> #<Hpricot::Doc {elem <html> {elem <body> {elem <p class="posted"> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p class="posted"> "foo" </p>} </body>} </html>}>
>> (doc/"p.posted").set(:class => 'newLinks')
=> #<Hpricot::Elements[{elem <p class="newLinks"> "A simple " {elem <b> "test" </b>} " string." </p>}, {elem <p class="newLinks"> "foo" </p>}]>