Forum: Ruby 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.
517b88ce9530ea20cce0218dfe349609?d=identicon&s=25 forest (Guest)
on 2006-03-16 18:29
I am new to ruby and still haven't got my mind around iterators although
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.

Here is the code in JavaScript:

var MAX_ROW = 3;
var MAX_COL = 3;
var nIdx = 0;

print("<table>");
for (var r = 0; nIdx < aList.length && r < MAX_ROW; r++)
{
	print("<tr>");
	for (var c = 0; nIdx < aList.length && c < MAX_COL; c++)
	{
		print("<td>");

		print(aList[nIdx].name);
		nIdx++;

		print("</td>");
	}	// cols
	print("</tr>");
}	// rows
print("</table>");


Can someone point me in the right direction for how to do this. I found
each_index but still was at a loss.

Thanks for the help.
forest
A402df36168b81b31c17adcbb5ae8cf4?d=identicon&s=25 Pistos Christou (pistos)
on 2006-03-16 18:53
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 is the code in JavaScript:
*snip*
> Can someone point me in the right direction for how to do this. I found
> each_index but still was at a loss.

Without giving it a lot of thought, I'd do something to the effect of:

rows = [
    [ 'col1', 'col2', 'col3' ],
    [ 'cOl1', 'Col2', 'col3' ],
    [ 'col1', 'COL2', 'COl3' ],
]

output = ''

output << '<table>'
rows.each do |row|
    output << '<tr>'
    row.each do |col|
        output << "<td>#{col}</td>"
    end
    output << '</tr>'
end
output << '</table>'

puts output
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-16 19:04
(Received via mailing list)
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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-16 20:15
(Received via mailing list)
Pistos Christou wrote:
> 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 is the code in JavaScript:
> *snip*
> > Can someone point me in the right direction for how to do this. I found
> > each_index but still was at a loss.

puts %w(1 2 3 4 5 6 7 8 9).map{|x| "<td>" + x + "</td>" }.
  join.gsub(/(<td.*?\/td>){3}/,"<tr>\n  \\&\n</tr>\n")
517b88ce9530ea20cce0218dfe349609?d=identicon&s=25 forest (Guest)
on 2006-03-16 20:46
Thanks all. The each_slice solution I think will work the best for my
scenario. I really appreciate the help.

forest


>  >> 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

> Hope that helps.
>
> James Edward Gray II
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-03-16 22:12
(Received via mailing list)
>  >> require "enumerator"

Is this standard library?

I tried 'ri Enumerator' to learn more, but it just comes back talking
about SyncEnumerator, which is something else.  (Trying the lowercase
'enumerator' didn't work at all.)
24d3102d656a4654db23d28382a2d6f0?d=identicon&s=25 Timothy Bennett (Guest)
on 2006-03-16 22:21
(Received via mailing list)
Yes, enumerator is part of the standard library.  Go to http://ruby-
doc.org/stdlib/ and find enumerator (right after English) in the left
frame if ri isn't helping.

Tim
714d07c320146d058fe42a489ef0e1d2?d=identicon&s=25 Leon Kovalenko (leon)
on 2008-01-28 12:29
I wrote next function (maybe need to be rewritten to method for Array
cass) -
  def array_to_table(arr, width)
    data = "<table>"
    arr.each_slice(width) do |row|
      data << "<tr>"
      row.each do |elem|
        data << ("<td>" + yield(elem) + "</td>")
      end
      data << "</tr>"
    end
    data << "</table>"
  end

Using -
<%= array_to_table(User.find(:all), 3) {|user| "<div
id='user_div'>#{user.name}</div>"} %>
This topic is locked and can not be replied to.