Helpers::tablify: fix FloatDomainError

Before this commit the following snippet didn't work:

  pry(main)> Pry::Helpers.tablify(['foobar', 'baz'], 5)
  #=> FloatDomainError

There was a divison by zero in `Helpers::Table#_recolumn`.

The problem is incorrectly written `until` loop condition. Note that the
longest element in the array has 6 characters. But the second argument
tells `::tablify` that the line width is only 5 characters long.

This commit changes the condition. Now, if you run the same snippet, you
would see that the elements form one column (and the code doesn't blow
up your program).

Possibly, in the described case, the table has unwanted blanks. However,
I'm not very competent in the code, so I'd better not touch it, because
it works.
This commit is contained in:
Kyrylo Silin 2013-01-27 06:12:35 +02:00
parent 584aea188f
commit 9074613927
2 changed files with 20 additions and 1 deletions

View File

@ -22,7 +22,7 @@ class Pry
def self.tablify(things, line_length)
table = Table.new(things, :column_count => things.size)
table.column_count -= 1 until 0 == table.column_count or
table.column_count -= 1 until 1 == table.column_count or
table.fits_on_line?(line_length)
table
end

View File

@ -80,6 +80,25 @@ asfadsssaaad fasfaafdssd s
end
end
describe 'line length is smaller than the length of the longest word' do
before do
element = 'swizzle'
@elem_len = element.length
@out = [element, 'crime', 'fun']
end
it 'should not raise error' do
should.not.raise(FloatDomainError) {
Pry::Helpers.tablify(@out, @elem_len - 1)
}
end
it 'should format output as one column' do
table = Pry::Helpers.tablify(@out, @elem_len - 1).to_s
table.should == "swizzle\ncrime \nfun "
end
end
describe 'decide between one-line or indented output' do
Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'
end