What is the difference between print and puts?
For example in this line of code I wrote, print
and puts
produce different results.
1.upto(1000).each { |i| print i if i % 2 == 0 }
Solution 1:
puts
adds a new line to the end of each argument if there is not one already.
print
does not add a new line.
For example:
puts [[1,2,3], [4,5,nil]]
Would return:
1 2 3 4 5
Whereas print [[1,2,3], [4,5,nil]]
would return:
[[1,2,3], [4,5,nil]]
Notice how puts does not output the nil value whereas print does.
Solution 2:
A big difference is if you are displaying arrays. Especially ones with NIL. For example:
print [nil, 1, 2]
gives
[nil, 1, 2]
but
puts [nil, 1, 2]
gives
1
2
Note, no appearing nil item (just a blank line) and each item on a different line.
Solution 3:
print
outputs each argument, followed by $,
, to $stdout
, followed by $\
. It is equivalent to args.join($,) + $\
puts
sets both $,
and $\
to "\n" and then does the same thing as print
. The key difference being that each argument is a new line with puts
.
You can require 'english'
to access those global variables with user-friendly names.
Solution 4:
The API docs give some good hints:
print() → nil
print(obj, ...) → nil
Writes the given object(s) to ios. Returns
nil
.The stream must be opened for writing. Each given object that isn't a string will be converted by calling its
to_s
method. When called without arguments, prints the contents of$_
.If the output field separator (
$,
) is notnil
, it is inserted between objects. If the output record separator ($\
) is notnil
, it is appended to the output....
puts(obj, ...) → nil
Writes the given object(s) to ios. Writes a newline after any that do not already end with a newline sequence. Returns
nil
.The stream must be opened for writing. If called with an array argument, writes each element on a new line. Each given object that isn't a string or array will be converted by calling its
to_s
method. If called without arguments, outputs a single newline.
Experimenting a little with the points given above, the differences seem to be:
-
Called with multiple arguments,
print
separates them by the 'output field separator'$,
(which defaults to nothing) whileputs
separates them by newlines.puts
also puts a newline after the final argument, whileprint
does not.2.1.3 :001 > print 'hello', 'world' helloworld => nil 2.1.3 :002 > puts 'hello', 'world' hello world => nil 2.1.3 :003 > $, = 'fanodd' => "fanodd" 2.1.3 :004 > print 'hello', 'world' hellofanoddworld => nil 2.1.3 :005 > puts 'hello', 'world' hello world => nil
-
puts
automatically unpacks arrays, whileprint
does not:2.1.3 :001 > print [1, [2, 3]], [4] [1, [2, 3]][4] => nil 2.1.3 :002 > puts [1, [2, 3]], [4] 1 2 3 4 => nil
-
print
with no arguments prints$_
(the last thing read bygets
), whileputs
prints a newline:2.1.3 :001 > gets hello world => "hello world\n" 2.1.3 :002 > puts => nil 2.1.3 :003 > print hello world => nil
-
print
writes the output record separator$\
after whatever it prints, whileputs
ignores this variable:mark@lunchbox:~$ irb 2.1.3 :001 > $\ = 'MOOOOOOO!' => "MOOOOOOO!" 2.1.3 :002 > puts "Oink! Baa! Cluck! " Oink! Baa! Cluck! => nil 2.1.3 :003 > print "Oink! Baa! Cluck! " Oink! Baa! Cluck! MOOOOOOO! => nil