You can display output to the screen in Ruby using print statements such as puts
, print
and p
. Here's how these methods work:
Method | Description |
---|---|
puts |
Prints with a newline at the end. |
print |
Prints without adding a newline at the end. |
p |
Prints in a technical way that's easy for debugging. |
Let's look at each of these methods one by one.
Ruby puts Method
The puts
method in Ruby prints content and automatically adds a newline at the end. For example,
# Print a sentence to the screen using puts
puts "Ruby tutorials for beginners!"
# Print another sentence to the screen
puts "Brought to you by Programiz!"
Output
Ruby tutorials for beginners! Brought to you by Programiz!
Here, we used two puts
statements to print two sentences.
As you can see, the second sentence is printed in a new line.
That's because puts
adds a newline at the end of the text it prints. Thus, the next print statement will print in the next line.
Ruby print Method
The print
method works similarly, but does not add a newline. This means subsequent output appears on the same line. For example,
# Print a sentence to the screen using print
print "Ruby tutorials for beginners!"
# Print another sentence to the screen
print "Brought to you by Programiz!"
# Output: Ruby tutorials for beginners!Brought to you by Programiz!
Here, we used two print
statements to print two sentences.
Because print
doesn't move the cursor to the next line, the second message appears immediately after the first.
Add a Newline with the print Method
You can manually insert a newline inside print by using the newline character \n
. For example,
# Add a newline character at the end of the string
print "Ruby tutorials for beginners!\n"
print "Brought to you by Programiz!"
Output
Ruby tutorials for beginners! Brought to you by Programiz!
Ruby p Method
The p
method is commonly used for debugging. It prints values in a developer-friendly way:
- Strings are shown with quotation marks.
- Other values (like numbers,
nil
,true
,false
, etc.) are shown as-is.
Example
# Print a string
p "Ruby tutorials for beginners!"
# Print a number
p 34
# Print 'nil' value
p nil
# Print boolean values 'true' and 'false'
p true
p false
Output
"Ruby tutorials for beginners!" 34 nil true false
As you can see, the string value is printed with quotations, but the other values are printed without them.
More on the Ruby p Method
The p
method is great for debugging because it prints the internal representation of an object.
In Ruby, the inspect
method gives the internal representation of an object.
Thus, the p
method prints the result of calling .inspect
on the object, which is Ruby's way of showing the internal, developer-friendly representation of data. For example,
greeting = "Hello, World"
# Print greeting using p
p greeting
# Print greeting using inspect
puts greeting.inspect
Output
"Hello, World" "Hello, World"
As you can see, p greeting
gives the same output as puts greeting.inspect
. Thus, printing with p
is equivalent to:
puts obj.inspect
Here, obj
is any object such as strings, numbers, booleans, arrays, class instances, etc.
Example 1: Comparing the Different Print Statements
Let's compare all three print statements using an example:
# Use puts to print a string
puts "Hello, World!"
# Use print to display a string
print "Hello, World! "
# Use p to print a string
p "Hello, World!"
Output
Hello, World! Hello, World! "Hello, World!"
Here,
puts
adds a newline.print
keeps output on the same line.p
shows the value clearly, including quotes.
Example 2: Printing Arrays
Ruby has an interesting difference in how arrays are printed with the various display methods. Let's demonstrate this with an example:
my_array = [1, 2, 3]
puts "Printing Array Using puts:"
puts my_array
print "\nPrinting Array Using print: "
print my_array
print "\n\nPrinting Array Using p: "
p my_array
Output
Printing Array Using puts: 1 2 3 Printing Array Using print: [1, 2, 3] Printing Array Using p: [1, 2, 3]
As you can see, the print
and p
methods display the array in array notation.
On the other hand, the puts
method displays each array element in separate lines.
Printing Multiple Items
Ruby allows you to print multiple items using a single statement. You can accomplish this in one of two ways:
- By feeding multiple arguments to the method using commas.
- By joining strings together using the
+
operator.
Note: Don't worry if you're not sure what "arguments" means — you'll learn that soon when we cover methods.
Let's explore these techniques next.
Print Multiple Items Using Commas
You can use commas to separate items you want to print. For example,
name = "Peter"
age = 29
puts "Commas with puts:"
puts "Hello ", name, age
puts "\nCommas with print:"
print "Hello ", name, age
puts "\n\nCommas with p:"
p "Hello ", name, age
Output
Commas with puts: Hello Peter 29 Commas with print: Hello Peter29 Commas with p: "Hello " "Peter" 29
Here, we printed the string "Hello "
and the variable name
and age
using the different printing statements. As you can see,
puts
andp
displayed the items in separate lines.print
displayed the items in the same line and without any space in between them.
Print Multiple Strings Using the + Operator
The +
operator joins (concatenates) the different strings you want to print. These strings will be joined in the same line, regardless of whether you use puts or print.
Note: The +
operator only joins strings. If you try to use it with a number, Ruby will either raise an error (when one operand is a string) or perform arithmetic addition (if both are numbers).
Example
name = "Peter"
puts "Concatenation with puts:"
puts "Hello " + name
puts "\nConcatenation with print:"
print "Hello " + name
puts "\n\nConcatenation with p:"
p "Hello " + name
Output
Concatenation with puts: Hello Peter Concatenation with print: Hello Peter Concatenation with p: "Hello Peter"
Here, we joined a string "Hello "
and a string variable name
using the +
operator. As you can see, the objects are joined in the same line.
Concatenation Only Works With Strings
Only strings can be concatenated. You also cannot concatenate a string with data of another type. For example,
age = 29
# Invalid: Joining a string and a number
puts "Age: " + age
# Output: no implicit conversion of Integer into String (TypeError)
# Invalid: Concatenating boolean values
print true + false
# Output: undefined method '+' for true (NoMethodError)
If all the operands are numbers, Ruby will add them arithmetically instead of concatenating them:
age = 29
# + will add the numbers instead of joining them
puts 12 + age
print 12 + age
p 12 + age
Output
41 4141
Here, the program prints the sum of 12 and the value stored in the age
variable. The result is 12 + 29
, which is 41.
You can fix the issue of failed concatenation by converting the different types to string. To do this, use the to_s
method:
age = 29
# Valid: age has been converted to string
puts "Age: " + age.to_s
# Valid: Both boolean values are now strings
puts true.to_s + false.to_s
# Concatenate two numbers by converting them to strings
puts 12.to_s + age.to_s
Output
Age: 29 truefalse 1229
String Interpolation: Print Text and Variables Together
In Ruby, you can insert variables directly into strings using interpolation with #{}
. This works only with double quotes. For example,
# Create name variable
name = "Ash Ketchum"
# Create age variable
age = 10
# Print the variables inside a string
puts "Hello, #{name}! You are #{age} years old."
# Output: Hello, Ash Ketchum! You are 10 years old.
Here,
#{name}
- Inserts the value of thename
variable inside the printed string.#{age}
- Inserts the value of theage
variable inside the printed string.
Note: In string interpolation, the values of the variables are inserted as strings. Thus, even though age
is a variable of number
type, Ruby automatically converts it to a string during interpolation.