Forum: Ruby Re: build 3x3 table from one array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Berger, Daniel (Guest)
on 2006-03-16 20:29
(Received via mailing list)
> I think they are really cool.
>
> 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.

<snip>

If you don't mind a 3rd party solution:

require 'html/table'
include HTML

array = [1,2,3,4,5,6,7,8,9]

table = Table.new
table.push(Table::Row.new(array[0..2]))
table.push(Table::Row.new(array[3..5]))
table.push(Table::Row.new(array[6..9]))

puts table.html

<table>
   <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>
</table>
This topic is locked and can not be replied to.