Class: WhoAmI::TextTable
- Inherits:
-
Object
- Object
- WhoAmI::TextTable
- Includes:
- Enumerable
- Defined in:
- lib/who_am_i/text_table.rb
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(join:, prefix: "", suffix: "") ⇒ TextTable
constructor
A new instance of TextTable.
- #push(row) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(join:, prefix: "", suffix: "") ⇒ TextTable
Returns a new instance of TextTable.
5 6 7 8 9 10 11 |
# File 'lib/who_am_i/text_table.rb', line 5 def initialize(join:, prefix: "", suffix: "") @join = join @prefix = prefix @suffix = suffix @rows = [] @column_lengths = [] end |
Instance Method Details
#each ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/who_am_i/text_table.rb', line 28 def each if !block_given? return enum_for(:each) end @rows.each do |row| output_fields = row.map.with_index do |field, index| field.to_s.ljust(@column_lengths[index], " ") end output_row = @prefix + output_fields.join(@join) + @suffix yield output_row end end |
#push(row) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/who_am_i/text_table.rb', line 13 def push(row) @rows.push(row) row.each.with_index do |field, index| @column_lengths[index] = if @column_lengths[index].nil? field.length else [@column_lengths[index], field.length].max end end true end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/who_am_i/text_table.rb', line 45 def to_s map(&:rstrip).join("\n") + "\n" end |