I have an array and my goal is to print the code in such a way that it puts out the following:

How can I do that?
I imagine there are lots of gems available for doing this, but if you want to roll-your-own, you could do it like this, in a fairly general way:
Your input is comprised of column labels:
col_labels = { date: "Date", from: "From", subject: "Subject" }
and the data for the rows:
arr = [{date: "2014-12-01", from: "Ferdous", subject: "Homework this week"},
{date: "2014-12-01", from: "Dajana", subject: "Keep on coding! :)"},
{date: "2014-12-02", from: "Ariane", subject: "Re: Homework this week"}]
where col_labels and the elements of arr have the same keys.
From this point on, the code is general. First construct a hash @columns (which I've made an instance variable for convenience).
@columns = col_labels.each_with_object({}) { |(col,label),h|
h[col] = { label: label,
width: [arr.map { |g| g[col].size }.max, label.size].max } }
# => {:date=> {:label=>"Date", :width=>10},
# :from=> {:label=>"From", :width=>7},
# :subject=> {:label=>"Subject", :width=>22}}
def write_header
puts "| #{ @columns.map { |_,g| g[:label].ljust(g[:width]) }.join(' | ') } |"
end
def write_divider
puts "+-#{ @columns.map { |_,g| "-"*g[:width] }.join("-+-") }-+"
end
def write_line(h)
str = h.keys.map { |k| h[k].ljust(@columns[k][:width]) }.join(" | ")
puts "| #{str} |"
end
write_divider
write_header
write_divider
arr.each { |h| write_line(h) }
write_divider
+------------+---------+------------------------+
| Date | From | Subject |
+------------+---------+------------------------+
| 2014-12-01 | Ferdous | Homework this week |
| 2014-12-01 | Dajana | Keep on coding! :) |
| 2014-12-02 | Ariane | Re: Homework this week |
+------------+---------+------------------------+
If you want to reverse the display and make it a bit larger, like yours, first execute:
$_!.reverse
$_@ += 4

The easiest way to do it is with the table_print gem: http://tableprintgem.com
For example, using the array from the other answer,
your_array = [
{date: "2014-12-01", from: "Ferdous", subject: "Homework this week"},
{date: "2014-12-01", from: "Dajana", subject: "Keep on coding! :)"},
{date: "2014-12-02", from: "Ariane", subject: "Re: Homework this week"},
]
tp your_array
you would get the following result:
DATE | FROM | SUBJECT
-----------|---------|-----------------------
2014-12-01 | Ferdous | Homework this week
2014-12-01 | Dajana | Keep on coding! :)
2014-12-02 | Ariane | Re: Homework this week
You can also specify the columns you want to include (as strings or symbols):
tp your_array, "subject", :from
This is very useful when your array is from an ActiveRecord query, and displaying every single column would make the output unreadable:
tp User.all, :full_name, :e-mail_address
You can even access related models:
tp User.all, :full_name, "posts.title"
arr = [{date: "2014-12-01", from: "Ferdous", subject: "Homework this week"}, {date: "2014-12-01", from: "Dajana", subject: "Keep on coding! :)"}, {date: 2014-12-02, from: "Madonna", subject: "Re: Homework this week"}].ActiveRecordtable? etc) and some things about your code (eg a sample of the relevant class) and the attempts you 've made so far.hirbgem github.com/cldwalker/hirb