Monday, August 27, 2012

Creating a node in Nokogiri with both an attribute and content

Using Nokogiri's builder is a quick way to generate an HTML page when required.

When creating a Node in ruby there can be a case where you

doc.h4 "Some text" 

generates:

<h4>"Some text"</h4>

doc.h4(:'text-align'=>"right")

(Note since text-align has a dash in the middle you will need to use quotes around the key name.)

generates:


<h4 text-align="right"/>


So how would you generate:

<h4 text-align="right">"Some Text"</h4>  ???

doc.h4(:'text-align'=>"right") "Some Text"

Results in:


./result2html.rb:73: syntax error, unexpected tSTRING_BEG, expecting '}'
              doc.h4(:'text-align'=>"right") "Some Text"


Reading the documentation turned up the answer:

doc.h4(:'text-align'=>"right") { doc.text "Some Text" }



Wednesday, June 27, 2012

Listing all available classes in ruby

I have to look further into the ObjectSpace module.

But this command lists all the available classes.

ObjectSpace.each_object(Class) {|c| p c}

You could also save all the results to an array, require your new module, call it again and subtract one set from another just to get the new classes from the library (If you're lacking documentation).