class Array::TableChars

string = ARRAY.to_table type

Convert an array of arrays to ASCII table representation

to create a box, we need to describe the parts of it: (be sure to used fixed-width font)

+-------+-------+  <== top border
| cell1 | cell2 |  <== data row
+-------+-------+  <== inner border
| r2c1  | r2c2  |  <== data row
+-------+-------+  <== bottom border
^       ^       ^
|       |       +--<== right
|       +----------<== middle
+------------------<== left

Given these, there are 15 specific characters for line drawing:

   Location          Symbol          Location          Symbol
================     ======       ================     ======
  top-left-border     tlb           left-inner-border   lib
  top-border           tb                inner-border    ib
  top-inner-border    tib         middle-inner-border   mib
  top-right-border    trb          right-inner-border   rib

  left-data-border    ldb          bottom-left-border   blb
 inner-data-border    idb          bottom-border         bb
 right-data-border    rdb          bottom-inner-border  bib
                                   bottom-right-border  brb

In the specs below, the characters are given in the same order as above. The characters are index by the given symbols.

Attributes

start[R]
stop[R]

Public Class Methods

get(name) click to toggle source

TableChars.get NAME

Return the TableChars object for NAME, or nil

# File lib/array/formatter.rb, line 134
def self.get(name)
  @chars = @@table_chars[name]
end
new(name, fmt_chars, start=nil, stop=nil) click to toggle source
# File lib/array/formatter.rb, line 112
def initialize(name, fmt_chars, start=nil, stop=nil)
  @chars = self
  @@table_chars[name] = @chars
  if fmt_chars.class == Symbol
    @@table_chars[name] = @chars = @@table_chars[fmt_chars].dup
  elsif fmt_chars.class == Array
    chars = fmt_chars.dup
    @@table_char_names.each{|n| @chars[n] = chars.shift || raise(ArgumentError, "Missing characters from table drawing charset!")}
  end
  @start = start
  @stop = stop
  @chars
end

Public Instance Methods

wrap(text) click to toggle source
# File lib/array/formatter.rb, line 140
def wrap(text)
  (@start || '') + text + (@stop || '')
end