aptinio / text-table

A feature-rich, easy-to-use plain text table formatter.
MIT License
96 stars 14 forks source link

Initialization of column_widths #8

Closed vanch closed 9 years ago

vanch commented 9 years ago

Hi.

I'm using text-table on Ubuntu 14.04 with Ruby 1.9.3. Getting the issue when initializing table with empty set. Example:

table = Text::Table.new(:head => ['A', 'B'], :rows => [['a1', 'b1'], ['a2', 'b2']])
puts table.to_s
+----+----+
| A  | B  |
+----+----+
| a1 | b1 |
| a2 | b2 |
+----+----+

...

t = Text::Table.new
t.head = ['A', 'B']
t.rows = [['a1', 'b1']]
t.rows << ['a2', 'b2']
puts t.to_s
NoMethodError: undefined method `+' for nil:NilClass
        from /usr/lib/ruby/vendor_ruby/text-table/cell.rb:46:in `cell_width'
        from /usr/lib/ruby/vendor_ruby/text-table/cell.rb:29:in `to_s'
        from /usr/lib/ruby/vendor_ruby/text-table/row.rb:24:in `map'
        from /usr/lib/ruby/vendor_ruby/text-table/row.rb:24:in `to_s'
        from /usr/lib/ruby/vendor_ruby/text-table/table.rb:169:in `map'
        from /usr/lib/ruby/vendor_ruby/text-table/table.rb:169:in `to_s'
        from (irb):141
        from /usr/bin/irb:12:in `<main>'

It looks like problem with initialization of column_widths variable because it's equal to [] in second try. Sorry, can't make a fix, i'm new to Ruby and your one-liner syntax is too hard for me.

aptinio commented 9 years ago

Thanks for submitting this. I think you're in a console which calls #inspect to display the last expression. Before Ruby 2.0.0, #inspect delegates to #to_s, so #to_s is being called before the rows are set. I've implemented #inspect to fix this and it'll be included in the next release. Thanks again.