Class: Gawk::Table

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

Instance Method Summary collapse

Constructor Details

#initialize(name: "", headings: [], rows: []) ⇒ Table

Returns a new instance of Table.



3
4
5
6
7
8
# File 'lib/gawk/table.rb', line 3

def initialize(name: "", headings: [], rows: [])
  @name = name
  @headings = headings
  @rows = rows
  @widths = get_widths
end

Instance Method Details

#headings=(arr) ⇒ Object



14
15
16
17
# File 'lib/gawk/table.rb', line 14

def headings=(arr)
  @headings = arr
  @widths = get_widths
end

#name=(str) ⇒ Object



10
11
12
# File 'lib/gawk/table.rb', line 10

def name=(str)
  @name = str
end

#rows=(arr) ⇒ Object



19
20
21
22
# File 'lib/gawk/table.rb', line 19

def rows=(arr)
  @rows = arr
  @widths = get_widths
end

#to_sObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gawk/table.rb', line 24

def to_s
  buffer = []
  buffer << ""
  buffer << "=" * width
  buffer << @name
  buffer << "-" * width
  buffer << buffer(@headings)
  buffer << ""
  @rows.each {|row| buffer << buffer(row)}
  buffer << ""
  buffer.join("\n")
end

#widthObject



37
38
39
# File 'lib/gawk/table.rb', line 37

def width
  (@widths.inject(0){|sum,x| sum + x } + (@widths.length - 1) * 6)
end