CodeNewbie Community 🌱

Discussion on: The Three Ways to Print in Ruby

Collapse
 
djuber profile image
Daniel Uber

There's also pp which is a pretty-printer defined in the standard library. The easiest example showing how it differs from p is probably on larger arrays:

# p just prints the elements as a comma separated list, 
# your console line wraps if it runs out of space
p (0..25).to_a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

# pp puts the elements in a left justified column when 
# the number of items is wider than a certain width, 
# for pp (0..20) I got the horizontal/compact version, but 25
#  is just big enough to cause the vertical display:
pp (0..25).to_a
[0,
 1,
 2,
 3,
 4,
...

 25]
Enter fullscreen mode Exit fullscreen mode