What is the difference between print and puts?

Ruby

Ruby Problem Overview


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 }

Ruby Solutions


Solution 1 - Ruby

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 - Ruby

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 - Ruby

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 - Ruby

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 not nil, it is inserted between objects. If the output record separator ($</code>) is not nil, 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) while puts separates them by newlines. puts also puts a newline after the final argument, while print 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, while print 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
    => nil2.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 by gets), while puts 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

    => nil 2.1.3 :003 > print hello world => nil

  • print writes the output record separator $\ after whatever it prints, while puts 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

Solution 5 - Ruby

puts call the to_s of each argument and adds a new line to each string, if it does not end with new line. print just output each argument by calling their to_s.

for example: puts "one two": one two

{new line}

puts "one two\n": one two

{new line} #puts will not add a new line to the result, since the string ends with a new line

print "one two": one two

print "one two\n": one two

{new line}

And there is another way to output: p >For each object, directly writes obj.inspect followed by a newline to the program’s standard output.

It is helpful to output debugging message. p "aa\n\t": aa\n\t

Solution 6 - Ruby

If you would like to output array within string using puts, you will get the same result as if you were using print:

puts "#{[0, 1, nil]}":
[0, 1, nil]

But if not withing a quoted string then yes. The only difference is between new line when we use puts.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeleteView Question on Stackoverflow
Solution 1 - RubymikewilliamsonView Answer on Stackoverflow
Solution 2 - RubyMBentleyView Answer on Stackoverflow
Solution 3 - RubywersimmonView Answer on Stackoverflow
Solution 4 - RubyMark AmeryView Answer on Stackoverflow
Solution 5 - RubyryanView Answer on Stackoverflow
Solution 6 - Rubyuser2273663View Answer on Stackoverflow