Class: LittleTable

Inherits:
Object
  • Object
show all
Defined in:
lib/little_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ LittleTable

Returns a new instance of LittleTable.



5
6
7
# File 'lib/little_table.rb', line 5

def initialize input
  @table_string = input
end

Instance Method Details

#hashesObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/little_table.rb', line 21

def hashes
  result = []
  rows.each do |row|
    row_hash = {}
    headers.each_with_index do |header, index|
      row_hash[header.parameterize('_').to_sym] = row[index]
    end
    result.push row_hash
  end
  result
end

#headersObject



13
14
15
# File 'lib/little_table.rb', line 13

def headers
  parse_line_number 0
end

#lengthObject



9
10
11
# File 'lib/little_table.rb', line 9

def length
  @table_string.lines.count - 1
end

#rowsObject



17
18
19
# File 'lib/little_table.rb', line 17

def rows
  @table_string.lines[1..-1].map { |line| parse_line line }
end