AnoE AnoE - 4 months ago 26
Ruby Question

More beautiful, indented, pretty-printing

Array.inspect
returns its output in a flat line:

aoa = [ [1,2,3], [4,5,6] ]
puts aoa.inspect # => [[1, 2, 3], [4, 5, 6]]


Is there an easy way to get an indented output instead? The exact format (e.g., whether there is a line break after the first
[
) is not important to me. I just would like to have it more readable.

Compare Perl:

DB<2> print Dumper([[1,2,3],[4,5,6]])
$VAR1 = [
[
1,
2,
3
],
[
4,
5,
6
]
];


The solution should support hashes as well and handle other things gracefully.

Answer

You might want to try the AwesomePrint gem which would return the following by default (the actual output is colored) and is customizable:

aoa = [ [1,2,3], [4,5,6] ]
#=> [
#     [0] [
#       [0] 1,
#       [1] 2,
#       [2] 3
#     ],
#     [1] [
#       [0] 4,
#       [1] 5,
#       [2] 6
#     ]
#   ]        
Comments