Class: Eco::CSV::Table
- Inherits:
-
CSV::Table
- Object
- CSV::Table
- Eco::CSV::Table
- Defined in:
- lib/eco/csv/table.rb
Instance Method Summary collapse
-
#add_column(header_name) ⇒ Eco::CSV::Table
Adds a new column at the end.
-
#columns ⇒ Array<Array>
Each array is the column header followed by its values.
-
#columns_hash ⇒ Hash
Keys are headers, values are arrays.
-
#initialize(input) ⇒ Table
constructor
A new instance of Table.
-
#length ⇒ Integer
Total number of rows not including the header.
- #rows ⇒ Array<::CSV::Row>
Constructor Details
#initialize(input) ⇒ Table
Returns a new instance of Table.
8 9 10 |
# File 'lib/eco/csv/table.rb', line 8 def initialize(input) super(to_rows_array(input)) end |
Instance Method Details
#add_column(header_name) ⇒ Eco::CSV::Table
Adds a new column at the end
32 33 34 35 |
# File 'lib/eco/csv/table.rb', line 32 def add_column(header_name) new_col = Array.new(length).unshift(header_name) columns_to_table(columns.push(new_col)) end |
#columns ⇒ Array<Array>
Returns each array is the column header followed by its values.
25 26 27 |
# File 'lib/eco/csv/table.rb', line 25 def columns to_a.transpose end |
#columns_hash ⇒ Hash
Note:
it will override columns with same header name
Returns keys are headers, values are arrays.
39 40 41 42 43 |
# File 'lib/eco/csv/table.rb', line 39 def columns_hash columns.map do |col| [col.first, col[1..-1]] end.to_h end |
#length ⇒ Integer
Returns total number of rows not including the header.
20 21 22 |
# File 'lib/eco/csv/table.rb', line 20 def length to_a.length - 1 end |
#rows ⇒ Array<::CSV::Row>
13 14 15 16 17 |
# File 'lib/eco/csv/table.rb', line 13 def rows [].tap do |out| self.each {|row| out << row} end end |