I know this is probably dead simple, but I've got some data such as this in one file:
Artichoke
Green Globe, Imperial Star, Violetto
24" deep
Beans, Lima
Bush Baby, Bush Lima, Fordhook, Fordhook 242
12" wide x 8-10" deep
that I'd like to be able to format into a nice TSV type of table, to look something like this:
Name | Varieties | Container Data
----------|------------- |-------
some data here nicely padded with even spacing and right aligned text
This is a reasonably full example that assumes the following
I am a bit of a noob to rails so there are undoubtedly better and more elegant ways to do this
The following sample file
Produced the following output
I wrote a gem to do exactly this: http://tableprintgem.com
another gem: https://github.com/visionmedia/terminal-table Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby.
I have a little function to print a 2D array as a table. Each row must have the same number of columns for this to work. It's also easy to tweak to your needs.
No one has mentioned the "coolest" / most compact way -- using the
%
operator -- for example:"%10s %10s" % [1, 2]
. Here is some code:Gives:
Here is the code made more readable:
Kernel.sprintf should get you started.