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]
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]
--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.
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