Module: Enumerable

Defined in:
lib/text-table/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#to_text_table(options = {}) ⇒ Object Also known as: to_table

Returns a new Text::Table object with the elements as the rows.

Options

:first_row_is_head:: when set to true, the first row becomes the table heading :last_row_is_foot:: when set to true, the last row becomes the table footer

Examples

 require 'rubygems'
 require 'text-table'

 array = [
   ['Student', 'Mid-Terms', 'Finals'],
   ['Sam', 94, 93],
   ['Jane', 92, 99],
   ['Average', 93, 96]
 ]

 puts array.to_text_table

 #    +---------+-----------+--------+
 #    | Student | Mid-Terms | Finals |
 #    | Sam     | 94        | 93     |
 #    | Jane    | 92        | 99     |
 #    | Average | 93        | 96     |
 #    +---------+-----------+--------+

 puts array.to_text_table(:first_row_is_head => true)

 #    +---------+-----------+--------+
 #    | Student | Mid-Terms | Finals |
 #    +---------+-----------+--------+
 #    | Sam     | 94        | 93     |
 #    | Jane    | 92        | 99     |
 #    | Average | 93        | 96     |
 #    +---------+-----------+--------+

 puts array.to_text_table(:first_row_is_head => true, :last_row_is_foot => true)

 #    +---------+-----------+--------+
 #    | Student | Mid-Terms | Finals |
 #    +---------+-----------+--------+
 #    | Sam     | 94        | 93     |
 #    | Jane    | 92        | 99     |
 #    +---------+-----------+--------+
 #    | Average | 93        | 96     |
 #    +---------+-----------+--------+


51
52
53
54
55
56
# File 'lib/text-table/enumerable.rb', line 51

def to_text_table(options = {})
  table = Text::Table.new :rows => self.to_a.dup
  table.head = table.rows.shift if options[:first_row_is_head]
  table.foot = table.rows.pop   if options[:last_row_is_foot]
  table
end