On 10/30/06, Elliot Winkler <elliot.winkler / gmail.com> wrote:
> #inspect would work if the array were simple, but the array I want to pretty
> print is not. I would give you the real case but it's really long, so a
> better example is:
>
> #!/usr/bin/ruby
> require 'pp'
> array = [
>     { :key1 => 'value1', :key2 => 'value2', :key3 => 'value3' },
>     1, 2, 3,
>     [
>         'foo', 'bar',
>         [
>             { :key1 => 'value1', :key2 => 'value2' },
>             'baz', 'quux'
>         ]
>     ]
> ]
> array2 = array.dup
> array2[4][2].push('a longer string', 'an even longer string')
>
> p array
> puts
> pp array
> puts
> p array2
> puts
> pp array2
>
> ==OUTPUT==
>
> [{:key1=>"value1", :key2=>"value2", :key3=>"value3"}, 1, 2, 3, ["foo",
> "bar", [{:key1=>"value1", :key2=>"value2"}, "baz", "quux"]]]
>
> [{:key1=>"value1", :key2=>"value2", :key3=>"value3"},
>  1,
>  2,
>  3,
>  ["foo", "bar", [{:key1=>"value1", :key2=>"value2"}, "baz", "quux"]]]
>
> [{:key1=>"value1", :key2=>"value2", :key3=>"value3"}, 1, 2, 3, ["foo",
> "bar", [{:key1=>"value1", :key2=>"value2"}, "baz", "quux", "longer string",
> "even longer string"]]]
>
> [{:key1=>"value1", :key2=>"value2", :key3=>"value3"},
>  1,
>  2,
>  3,
>  ["foo",
>   "bar",
>   [{:key1=>"value1", :key2=>"value2"},
>    "baz",
>    "quux",
>    "longer string",
>    "even longer string"]]]
>
> For array2, I want 1, 2, and 3 to be on one line, "foo" and "bar" to be one
> line, and "baz", "quux" and "longer string" to be one line. PP isn't doing
> that, obviously.
>
> Perhaps someone knows of another pretty print library?

I thought that changing Array#pretty_print would be sufficient, after
deeper look it seems that the problem is in the prettyprint module -
somewhere in the breaking routines - if the whole group (=array,
hash,...) is short enough to fit within the desired width (default
=79) then it's not split into line. Otherwise *every* line is split.
So it's a kind of take all or nothing deal.

The solution would be (better, harder) patching prettyprint.rb to
check the individual fields for overflow or (hack, posissibly easier)
to patch Array#pretty_print from pp.rb to add breakable just in the
desired places.