Class: Array

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

Overview

Extensions to Array

Instance Method Summary collapse

Instance Method Details

#self_analyzeObject



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/textify.rb', line 291

def self_analyze
  @minimal_element, @maximal_element = nil,nil
  @min_elem_sz, @max_elem_sz = 0, 0
  each do |row|
    if row.is_a? Array
       row.each do |element|
         if element.to_s.size > @max_elem_sz
          @maximal_element = element; @max_elem_sz = element.to_s.size
         elsif element.to_s.size < @min_elem_sz
          @minimal_element = element; @min_elem_sz = element.to_s.size
         end
       end
    end
  end
  # puts "[Array.self_analyze] Maximal element => '#{@maximal_element}' (#{@max_elem_sz}), minimal element => '#{@minimal_element}'"
end

#table(options = {}) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/textify.rb', line 308

def table options = {}
  width = options[:width] || DefaultScreenWidth
  self_analyze
  
  columns = self[0].size
  
  min_column_width = @maximal_element.to_s.size + 2
  column_width = [min_column_width, ((width.to_i-columns).to_f/columns)].max.to_i    
  width = (column_width*columns) + columns + 1
  
  hb = ("*"*(width))+"\n"      
  result = hb

  # puts "[Array.table] Columns = #{columns}. Total width = #{width}. Column width = #{column_width}."

  if options[:headings] then
    options[:headings].each{ |th| result += "*#{justify :center, th.to_s, :width => column_width}"}
    result += "*\n"
    result += hb              
  end
  each do |row|
    row.each do |item|
      result += "*#{justify :center, item.to_s, :width => column_width}"
    end
    result += "*\n"
  end
  result += hb
  
  puts result
end