Class: LittleTable

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

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ LittleTable



7
8
9
# File 'lib/little_table.rb', line 7

def initialize input
  @table_string = input
end

Instance Method Details

#hashesObject



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

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



15
16
17
# File 'lib/little_table.rb', line 15

def headers
  parse_line_number 0
end

#lengthObject



11
12
13
# File 'lib/little_table.rb', line 11

def length
  @table_string.lines.count - 1
end

#rowsObject



19
20
21
# File 'lib/little_table.rb', line 19

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