Generate an HTML table from an array of hashes in Ruby

Posted by Horace Loeb on Stack Overflow See other posts from Stack Overflow or by Horace Loeb
Published on 2010-04-13T23:59:03Z Indexed on 2010/04/14 0:03 UTC
Read the original article Hit count: 328

Filed under:
|
|

What's the best way (ideally a gem, but a code snippet if necessary) to generate an HTML table from an array of hashes?

For example, this array of hashes:

[{"col1"=>"v1", "col2"=>"v2"}, {"col1"=>"v3", "col2"=>"v4"}]

Should produce this table:

<table>
  <tr><th>col1</th><th>col2</th></tr>
  <tr><td>v1</td><td>v2</td></tr>
  <tr><td>v3</td><td>v4</td></tr>
</table>

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails