There are many similarities in Ruby of puts vs print commands. They both basically carry out the same function of printing to the console. There are two main differences between puts vs print that we will focus on. In short, puts will add a newline character at the end, so that all printed calls are made on a new line. The print command will not do this and prints all of its calls on one line.

The second main difference is seen when printing out arrays. The puts command will print each item in the array on a new line, while the print command will simply print the array in its array form. The puts command will also display some values differently than the print command when it comes to displaying arrays.

So the best way to see the differences between these two commands is with some simple examples.

In this example, we will simply print the same sting twice to see how puts and print each do this.

number_one = "one"
number_two = "two"

puts number_one
puts number_two
print number_one
print number_two

#Output
one
two
onetwo

As you can see, each puts command printed on a new line, where print printed both strings on the same line.

Let’s switch the order of these to see what would happen.

number_one = "one"
number_two = "two"

print number_one
print number_two
puts number_one
puts number_two

#Output
onetwoone
two

You can see in this example, that the first puts call did not place its content on a new line. Remember that puts enters a newline after its returned value, so this is why it is on the same line as the print command returned values.

This is why we usually don’t want to mix our print and puts commands. Try to choose one or the other when you can.

Let’s take a look at the other main difference, printing arrays. The best way is to just show a bunch of examples.

some_array = ["This", "is", "an", "array", "of", "strings"]

puts some_array
print some_array

#Output
This
is
an
array
of
strings
["This", "is", "an", "array", "of", "strings"]

As you can see, the puts method will print each item of the array on a new line, while print just prints the array as is. We much prefer the print command when printing arrays.

Let’s see some more examples of both printing arrays with different values, to see what they return.

some_array = ["some_string", nil, "word", 3, true, "true", "", "done"]

puts some_array
print some_array

#Output
some_string

word
3
true
true

done
["some_string", nil, "word", 3, true, "true", "", "done"]

Notice what values puts returns for nils and empty strings in an array vs what print returns.

Finally, let’s see what both commands return for a simple hah.

some_hash = {"first_name" => "Sarah", "last_name" => "Smith", "age" => 40}

puts some_hash
print some_hash

#Output
{"first_name"=>"Sarah", "last_name"=>"Smith", "age"=>40}
{"first_name"=>"Sarah", "last_name"=>"Smith", "age"=>40}

With hashes, they both print the same thing.

Make sure to also check out the p command to see how that differs from puts and print.

Hopefully this article has been helpful for you to learn how about Ruby puts vs print.

Categorized in:

Ruby,

Last Update: March 1, 2024