我有一個Ruby散列,例如:Ruby 1.9的解析散成HTML表格
{"monkeys"=> {"hamburgers" => ["love", "eat"],
"boulders" => ["hate", "throw"]},
"stonemasons" => {"boulders" = > ["love", "use"],
"vandals" => ["hate", "sue"]}
}
它可以有幾乎任何級別(我可以把哈希散列內任意次數)的深度。它始終具有數組作爲最終值。
如何在不使用Rails的情況下將它解析爲如下所示的HTML表格,並且最好只使用標準庫?
<table>
<tr>
<th rowspan="2">monkeys</th>
<th>hamburgers</th>
<td>love</td>
<td>eat</td>
</tr>
<tr>
<th>boulders</th>
<td>hate</td>
<td>throw</td>
</tr>
<tr>
<th rowspan="2">stonemasons</th>
<th>boulders</th>
<td>love</td>
<td>use</td>
</tr>
<tr>
<th>vandals</th>
<td>hate</td>
<td>sue</td>
</tr>
</table>
希望這種聯繫將幫助你... HTTP://stackoverflow.com/questions/4588196/turn- a-ruby-hash-into-html-list – LHH
它在一定程度上有所幫助,但主要問題在於:1)計算羣體; 2)找到新的tr並妥善放置它們。 –
看起來很奇怪,你想要一些'tr'有2'th'和2'td',而其他人只有'th'和2'td'。你確定你不需要爲結果表使用'rowspan'而不是'colspan'嗎? –