Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/textify.rb
Overview
Extensions to Hash
Instance Method Summary collapse
-
#self_analyze ⇒ Object
include Textual.
- #table(options = {}) ⇒ Object
Instance Method Details
#self_analyze ⇒ Object
include Textual
253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/textify.rb', line 253 def self_analyze @minimal_element, @maximal_element = nil,nil @min_elem_sz, @max_elem_sz = 0, 0 each_pair do |k,v| [k,v].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 # puts "[Hash.self_analyze] Maximal element => '#{@maximal_element}' (#{@max_elem_sz}), minimal element => '#{@minimal_element}'" end |
#table(options = {}) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/textify.rb', line 268 def table ={} self_analyze width = [:width] || DefaultScreenWidth min_column_width = @maximal_element.to_s.size + 2 column_width = [min_column_width, (width-3).to_f/2].max.to_i width = column_width*2 + 3 hb = "*"*width+"\n" result = hb each_pair{|k,v| result += "*#{justify :center, k.to_s, :width => (column_width)}*#{justify :center, v, :width=>(column_width)}*\n"} result += hb puts result end |