looping through a 2d array in ruby to display it in a table format?

Posted by Sean on Stack Overflow See other posts from Stack Overflow or by Sean
Published on 2011-03-06T02:05:20Z Indexed on 2011/03/20 0:10 UTC
Read the original article Hit count: 295

Filed under:

Hi

How can i represent a 2d array in a table format in the terminal, where it lines up the columns properly just like a table?

so it looks like so:

         1       2       3          4          5
   1 [ Infinity | 40 | 45       | Infinity | Infinity ]
   2 [ Infinity | 20 | 50       | 14       | 20 ]
   3 [ Infinity | 30 | 40       | Infinity | 40 ]
   4 [ Infinity | 28 | Infinity | 6        | 6 ]
   5 [ Infinity | 40 | 80       | 12       | 0 ]

instead of:

[ Infinity,40,45,Infinity,Infinity ]
[ Infinity,20,50,14,20 ]
[ Infinity,30,40,Infinity,40 ]
[ Infinity,28,Infinity,6,6 ]
[ Infinity,40,80,12,0 ]

© Stack Overflow or respective owner

Related posts about ruby