James Gray
3/16/2006 6:03:00 PM
On Mar 16, 2006, at 11:29 AM, forest wrote:
> I am stuck trying to figure out how to take an array of data and
> dynamically build a 3x3 html table from it without standard for loops.
Here's one idea:
>> data = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
>> require "enumerator"
=> true
>> puts "<table>"
<table>
=> nil
>> data.each_slice(3) do |row|
?> puts " <tr>"
>> row.each { |cell| puts " <td>#{cell}</td>" }
>> puts " </tr>"
>> end
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
=> nil
>> puts "</table>"
</table>
=> nil
Hope that helps.
James Edward Gray II