Class: MDTable
- Inherits:
-
Object
- Object
- MDTable
- Defined in:
- lib/mdtable.rb
Constant Summary collapse
- VERSION =
'0.0.1'
Class Method Summary collapse
-
.convert(rows) ⇒ Object
convert 2D array to md table array.
Instance Method Summary collapse
- #col_max_lengths ⇒ Object
-
#initialize(rows) ⇒ MDTable
constructor
A new instance of MDTable.
- #to_md ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(rows) ⇒ MDTable
Returns a new instance of MDTable.
22 23 24 |
# File 'lib/mdtable.rb', line 22 def initialize rows @rows = rows end |
Class Method Details
.convert(rows) ⇒ Object
convert 2D array to md table array
rows - array of array
Examples:
MDTable.convert [[1,2,3], ['x', 'x', 'x']]
# => ['1 | 2 | 3',
'- | - | -',
'x | x | x']
Returns an array of string as a markdown table
17 18 19 20 |
# File 'lib/mdtable.rb', line 17 def self.convert(rows) rows = rows.map {|row| row.map(&:to_s) } new(rows).to_md end |
Instance Method Details
#col_max_lengths ⇒ Object
33 34 35 36 37 |
# File 'lib/mdtable.rb', line 33 def col_max_lengths @col_max_lengths ||= 0.upto(@rows.first.size - 1).map {|i| @rows.map {|r| r[i].length }.max } end |
#to_md ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mdtable.rb', line 26 def to_md rows = @rows.dup.insert(1, col_max_lengths.map {|l| '-' * l }) rows.map {|row| row.each_with_index.map {|item, i| item.ljust(col_max_lengths[i]) }.join ' | ' } end |
#width ⇒ Object
39 40 41 |
# File 'lib/mdtable.rb', line 39 def width width ||= col_max_lengths.inject(&:+) + 4 + 4 end |