File: enumerable.rb

package info (click to toggle)
ruby-text-table 1.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 805; makefile: 5
file content (58 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Enumerable

  # Returns a new Text::Table object with the elements as the rows.
  #
  # ==== Options
  # <tt>:first_row_is_head</tt>:: when set to <tt>true</tt>, the first row becomes the table heading
  # <tt>:last_row_is_foot</tt>:: when set to <tt>true</tt>, 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     |
  #      #    +---------+-----------+--------+
  #
  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
  alias_method :to_table, :to_text_table unless method_defined? :to_table
end