In Ruby, there are three main ways to print the result of an operation or expression to the console. This is very similar to
All three of these commands to roughly the same thing, however there are some subtle differences between them.
puts: this prints the result without any type formatting (number vs. string for instance) and each
puts statement automatically inserts a new line between them. If you like a nice orderly format for printing outputs, the
puts command is it!
p: this is somewhere in the middle and can be very useful. The
p command will print each new output on a new line, but it will also print the output in the style of it's type. If you print
42 as a string, the output will be
"42" and if you print
42 as a number, it will print out as
Personally, I will use all three and sometimes will switch them out as needed. I usually use
p when printing out arrays, since the
puts command will print each index of an array on a different line, which takes up a lot of space.