[Ruby] Difference between print, puts and p

Introduction

Print, puts, p are prepared as functions for outputting a character string to the console in Ruby, but I tried to summarize the differences in my own way. Confirmed version: ruby 2.6.6p146 (2020-03-31 revision 67876) [x64-mingw32]

Experiment with the display

print print prints the string as it is. If an object is passed, print the string returned by the object's .to_s method.

print "abc"
print "def"

# => abcdef

For the line feed character \ n and the tab character \ t, if you enclose it in single quotes ', the\is escaped and displayed as characters, and if you enclose it in double quotes ", it is interpreted as a newline and tab, respectively. indicate.

print 'a\nb\tc'
# => a\nb\tc    #Do not interpret line breaks and tabs with single quotes

print "d\ne\tf"
# => d          #Double quotes interpret line breaks and tabs
# => e       f

If an array or hash is specified, it will be displayed in the defined format.

a = [ 1, 2, 3, "4", "5", "abc" ]
b = [ a: "a1", b: "b1", c: "c1" ]
c = [ [1,2], [3,4],5 ]
print a,b,c

# => [1, 2, 3, "4", "5", "abc"][{:a=>"a1", :b=>"b1", :c=>"c1"}][[1, 2], [3, 4], 5]

puts puts is displayed in the same way as print. The big difference is that a line break is added at the end. The same applies to the handling of single quotes and double quotes.

print "abc"
print "def"

# => abc
# => def

puts 'a\nb\tc'
# => a\nb\tc    #Do not interpret line breaks and tabs with single quotes

puts "d\ne\tf"    
# => d          #Double quotes interpret line breaks and tabs
# => e       f

If you specify an array or hash, the output will be very different from print. The array is expanded and output with line breaks for each element, and the character string is not quoted. In the case of hash, it is displayed without square brackets []. In a nested array, the elements are displayed separately, and the nested information cannot be grasped.

a = [ 1, 2, 3, "4", "5", "abc" ]
b = [ a: "a1", b: "b1", c: "c1" ]
c = [ [1,2], [3,4],5 ]
puts a,b,c

# => 1                               #All elements are expanded and displayed
# => 2
# => 3
# => 4                               #String quotes are not displayed.
# => 5
# => abc
# => {:a=>"a1", :b=>"b1", :c=>"c1"}  #There are no square brackets.
# => 1                               #All nested arrays are expanded at the same level
# => 2
# => 3
# => 4
# => 5

p p prints the string returned by the .inspect method instead of .to_s when the object is passed. In addition, a line break is added at the end of each line. The difference is that the character string is enclosed in double quotes and output.

p "abc"
p "def"

# => "abc"
# => "def"

The line feed character \ n and the tab character \ t are not interpreted as a line feed or tab. However, if you enclose it in single quotes ', the escaped version of\is displayed, and if you enclose it in double quotes ", the characters are displayed as they are without escaping.

p 'a\nb\tc'
# => "a\\nb\\tc"    # \To escape\\Will be.

p "d\ne\tf"
# => "d\ne\tf"    #Double quotes interpret line breaks and tabs

If an array or hash is specified, it will be displayed in the defined format.

a = [ 1, 2, 3, "4", "5", "abc" ]
b = [ a: "a1", b: "b1", c: "c1" ]
c = [ [1,2], [3,4],5 ]
p a,b,c

# => [1, 2, 3, "4", "5", "abc"]
# => [{:a=>"a1", :b=>"b1", :c=>"c1"}]
# => [[1, 2], [3, 4], 5]

What I found

--puts, p automatically add a newline at the end, but print does not add a newline at the end. --p is output with the character string enclosed in double quotes. --print, puts interprets and displays special characters such as line breaks \ n and tab \ t, but p does not interpret special characters and outputs them as character strings. .. --print, p prints arrays and hashes as defined, but puts expands arrays and prints them with line breaks for each element.

Summary

At first, I thought that puts was a version with line breaks in print, but there was a big difference in the array display. If you want to output the entire array and check it, it seems better not to use puts. In books and on the net, there are many descriptions that " p is for debugging ", but it is certainly easier to see if you do not interpret special characters when debugging, it is easy to distinguish between character strings and numerical values, and it is easy to input. So it's certainly better to check with the p function in debugging.

Recommended Posts

[Ruby] Difference between print, puts and p
Note: Difference between Ruby "p" and "puts"
[Ruby] Difference between puts and return, output and return value
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Difference between Ruby instance variable and local variable
Ruby print puts p printf output method
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between redirect_to and render
About the difference between classes and instances in Ruby
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
The difference between puts and print in Ruby is not just the presence or absence of line breaks
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
[Ruby] Difference between match / scan
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
The difference between programming with Ruby classes and programming without it
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[For Ruby beginners] What is the difference between puts print p? Actually, there is a clear way to use it properly!
Easy to understand the difference between Ruby instance method and class method.
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between pop () and peek () in stack