Class: ProxyRb::SimpleTable
- Inherits:
-
Object
- Object
- ProxyRb::SimpleTable
- Defined in:
- lib/proxy_rb/simple_table.rb
Overview
Generate simple table
Instance Method Summary collapse
-
#initialize(hash, opts = {}) ⇒ SimpleTable
constructor
Create.
-
#to_s ⇒ String
Generate table.
Constructor Details
#initialize(hash, opts = {}) ⇒ SimpleTable
Create
16 17 18 19 20 21 |
# File 'lib/proxy_rb/simple_table.rb', line 16 def initialize(hash, opts = {}) @hash = Hash(hash) @opts = { sort: true }.merge opts end |
Instance Method Details
#to_s ⇒ String
Generate table
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/proxy_rb/simple_table.rb', line 27 def to_s longest_key = hash.keys.map(&:to_s).max_by(&:length) return '' if longest_key.nil? name_size = longest_key.length rows = hash.each_with_object([]) do |(k, v), a| a << format("# %-#{name_size}s => %s", k, v) end if opts[:sort] == true rows.sort.join("\n") else rows.join("\n") end end |