# Convert 2D array to cute text table with | pipes?

**URL:** <https://www.ruby-forum.com/t/convert-2d-array-to-cute-text-table-with-pipes/169348>\
**Category:** Ruby\
**Created:** [June 2, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/convert-2d-array-to-cute-text-table-with-pipes/169348 "2009-06-02T18:45:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [June 2, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/convert-2d-array-to-cute-text-table-with-pipes/169348/1 "2009-06-02T18:45:39Z")

</div>

Ruboids:

I have only started googling I will report back here what I find or  
write.

Does anyone have a code snippet which reads a 2D array and outputs a  
cute  
formatted text table, like this?

```
   | concept | frobs | zones |
   | Unruly Pygmy | 6 | 10001 |
   | Renonymize | 4 | 9999 |
   | Opramatic | 4 | 10001 |
   | El Pollo Psycho | 4 | 10002 |
   | Mouse Guard | 4 | 10003 |
   | layuhponit | 4 | 10004 |

```

Please notice I’m not asking how to format text with sprintf(’%\*i’) etc.  
I’m  
asking if anyone has solved this problem in some cute generic way that  
makes for  
a good Ruby exercise.

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [June 2, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/convert-2d-array-to-cute-text-table-with-pipes/169348/2 "2009-06-02T19:16:14Z")

</div>

Have a look at Ruport’s text table rendering code

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [June 2, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/convert-2d-array-to-cute-text-table-with-pipes/169348/3 "2009-06-02T19:46:05Z")

</div>

Brian C. wrote:

> Have a look at Ruport’s text table rendering code

txbut - I should’a made clear I have no mandate to install a whole new  
library.  
Does it contain a snippet like this?

def row\_to\_pipes(row, widths)  
widths = widths.dup

```
 return ' ' + row.map{|c|
       case c
         when Fixnum
           c.to_s.rjust(widths.shift)
         else
           c.to_s.ljust(widths.shift)
       end
     }.join(' | ') + ' '

```

end

def table\_to\_pipes(table)  
widths = (0 … table.first.length).map do |x|  
table.sort\_by{|q| q[x].to\_s.length }.last[x].to\_s.length  
end

```
 table.map{|r| row_to_pipes(r, widths) }.join("\n")

```

end

test ‘array to pipes’ do  
assert\_equal ’ a | b | c ', row\_to\_pipes(%w(a b c), [1, 2, 1])  
end

test ‘table to pipes’ do  
assert\_equal " a | bb | c \n d | 2 | f ",  
table\_to\_pipes([%w(a bb c), [‘d’, 2, ‘f’]])  
end

Understand I would never write code this sloppy in production. Really!  
I’m just  
letting it out here so if anyone can think of a way to refactor it and  
DRY it  
up, we could make a little good-code experiment out of it.

It provides the effect I’m after:

```
 puts table_to_pipes( [[' __concept__', ' __frobs', '__ zones'],
                       ['Unruly Pygmy', 6, 10001],
                       ['Renonymize', 4, 9999],
                       ['Opramatic', 4, 10001],
                       ['El Pollo Psycho', 4, 10002],
                       ['Mouse Guard', 4, 10003],
                       ['layuhponit', 4, 10004]] )

```

**concept** | \_\_frobs | \_\_zones  
Unruly Pygmy | 6 | 10001  
Renonymize | 4 | 9999  
Opramatic | 4 | 10001  
El Pollo Psycho | 4 | 10002  
Mouse Guard | 4 | 10003  
layuhponit | 4 | 10004

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [June 3, 2009, 2:26pm UTC](https://www.ruby-forum.com/t/convert-2d-array-to-cute-text-table-with-pipes/169348/4 "2009-06-03T14:26:02Z")

</div>

I wrote an implementation using the Matrix class, but it didn’t really  
make it any shorter/cleaner/DRYer. I think your code is fairly DRY  
already. The only thing I might suggest is extracting your  
justification, e.g.

def justify(obj,w)  
case obj  
when Fixnum  
obj.to\_s.rjust(w)  
else  
obj.to\_s.ljust(w)  
end  
end
