• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Ruby puts vs print

Ruby puts vs print

October 18, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Get the Sum of an Array in Ruby
  • 2.  Using Ruby to Get the Length of Array
  • 3.  How to Square a Number in Ruby
  • 4.  Using Ruby to Create List of Prime Numbers
  • 5.  Ruby has_key Method – Check if Hash Has Key
  • 6.  Using Ruby to Reverse String
  • 7.  Ruby String Comparison
  • 8.  Ruby Floor – Find Floor of Number with floor Method
  • 9.  Using Ruby to Capitalize First Letter of Each Word
  • 10.  How to Print Without Newline in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy