P, Print, and Puts in Ruby
Jul 21, 2021
Unlike other programming languages, Ruby has p, print, puts to print out information to the command line. For Ruby beginners, it might be a little bit confusing to figure out when to use what.
print calls to_s on the object, it doesn’t append a new line.
> print 1,2,3
123 => nil
puts call to_s and add a new line to the output
> puts 1,2,3
1
2
3
=> nil
p calls inspect method and adds a new line. p is more useful for debugging, as it returns the original data type.
> p 1,2,3
1
2
3
=> [1,2,3]
In short: