Ruby on Rails / Rubygems / FullStack / Git / Mac notes.
Generate Html Table From Array or Hash
install gem builder
1
$ gem install builder
Use Builder for generate HTML table
1234567891011
# Use the XMLBuilder for this:require'builder'data=[{"col1"=>"v1","col2"=>"v2"},{"col1"=>"v3","col2"=>"v4"}]xm=Builder::XmlMarkup.new(:indent=>2)xm.table{xm.tr{data[0].keys.each{|key|xm.th(key)}}data.each{|row|xm.tr{row.values.each{|value|xm.td(value)}}}}puts"#{xm}"