Stu MacDonald bio photo

Stu MacDonald

Developer. Engineer. Hobbyist.

Email LinkedIn Github Stackoverflow
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" }