Table of contents
1.
Introduction
2.
Ruby Puts Method
3.
Printing in Ruby using Puts Method
3.1.
Printing Multiple Values on Separate Lines
3.2.
Concatenating Strings
3.3.
Interpolation using puts
3.4.
Array as Argument
3.5.
Miscellaneous Printing
4.
Redirecting Output to a File
5.
Difference between Puts and Print
6.
Frequently Asked Questions
6.1.
What happens when an object is passed to the puts method?
6.2.
Does puts method in Ruby return any value?
6.3.
Can puts output to a file redirect instead of printing it to the console?
6.4.
What happens nil value is passed to puts?
7.
Conclusion 
Last Updated: Mar 27, 2024
Easy

puts in Ruby

Author Ayush Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The 'puts' method plays an essential role in Ruby programming for displaying output to the console. Understanding how to use 'puts' correctly can significantly improve the readability and user engagement of your code, regardless of whether you are a beginner or experienced in Ruby programming.

puts in Ruby

In this blog, we will discuss various techniques and examples to understand the working of puts in Ruby programming language.

Ruby Puts Method

The 'puts' is a built-in Ruby method for displaying output to the console. It stands for "put string" and is mainly used to output strings and objects as readable text. Using their 'to_s' method, it automatically converts objects to strings. 

In the Ruby programming, "puts" is frequently used to print data followed by newline characters, show user prompts, and debug code. It accepts multiple arguments and prints on a new line.

A puts method in Ruby can handle multiple data types such as array, boolean, and numbers. It converts these values into strings before displaying them.

The basic syntax of the puts method in Ruby is:-

puts object
You can also try this code with Online Ruby Compiler
Run Code

Printing in Ruby using Puts Method

In this section, we will see variations and techniques for printing output using the puts method in Ruby.

Printing Multiple Values on Separate Lines

A puts in Ruby print multiple values output on different lines. Users can use various puts to print output in a separate line.

Example

puts "Coding"
puts "Ninjas"
You can also try this code with Online Ruby Compiler
Run Code


Output

Output

Explanation

In this example, puts methos print output on the different lines. The "Coding" and "Ninjas" is printed on separate lines.

Concatenating Strings

Users can concatenate strings using the ‘+’ operator and print the output using the puts in Ruby.

Example

puts "Coding" + " Ninjas"
You can also try this code with Online Ruby Compiler
Run Code


Output

output

Explanation

In this example, puts method print the Coding Ninjas after concatenating with the '+' operator.

Interpolation using puts

Ruby's string interpolation technique provides a simple way to create dynamic strings by allowing you to embed expressions, variables, or method directly calls within a string. It is denoted by ‘#[]’.

Users can insert values using string interpolation and print them using puts methods.

Example

company = "Ninjas"
puts "Coding, #{company}!"
You can also try this code with Online Ruby Compiler
Run Code


Output

Output

Explanation

In this example, "Ninjas" is given as a value in the company and is printed by the puts method.

Array as Argument

A puts in Ruby will print every element of the array on a single line when we pass an array as an argument.

Example

puts ["Coding","Ninjas"]
You can also try this code with Online Ruby Compiler
Run Code


Output

Output

Explanation

In this example, puts methods print array argument on different lines.

Miscellaneous Printing

Let's understand some miscellaneous printing using the puts method.

Example 1

puts “*”*5
You can also try this code with Online Ruby Compiler
Run Code


Output

Output

Explanation

In this example, put will print “.” 5 times.

 

Example 2

puts "Ninjas"*3
You can also try this code with Online Ruby Compiler
Run Code


Output

output

Explanation

In this example, Ninjas is printing three times without any separators.

Redirecting Output to a File

In Ruby, User can easily redirect the output of the “puts” method to the file instead of redirecting to the console. To redirect the output to the file, open a file in write mode and print the argument using the “puts” method.

Example

file = File.open("output.txt", "w")
$stdout = file

print "Writing inside the file"
puts "Coding, Ninjas!"
puts "I am an expert in Ruby Programmer."
print "Writing Done"

file.close
You can also try this code with Online Ruby Compiler
Run Code


Explanation

In this example, the “puts” method will save the output inside the “output.txt” file.

Difference between Puts and Print

Let's see some differences between the puts and print methods in Ruby.

puts

print

It is used to print output with a newline character at the end. It is used to print output without a newline character at the end.
It is used to print each value on separate lines. It is used to print each value on the same line.
It returns nil on the new line. It returns nil on the same line.
It accepts multiple arguments and print output on a separate line. It accepts multiple arguments and print output on the same line without any separators.
A puts method appends a new line automatically. A print method does not append a new line automatically.

Example:

puts "Coding"

puts "Ninjas"

Output:

Coding

Ninjas

Example:

print "Coding"

print "Ninjas"

Output:

CodingNinjas

Frequently Asked Questions

What happens when an object is passed to the puts method?

In Ruby, the to_s function of any object passed to puts is automatically invoked to transform the object into a string representation. To enable puts to print the object as output, the to_s method converts the object to a string. 

Does puts method in Ruby return any value?

Yes, after printing the output, puts returns nil. The return value of nil indicates that puts has no valuable output to return and is just used for the console reporting.

Can puts output to a file redirect instead of printing it to the console?

Yes, you can reroute the output by directly specifying the file to write to or by using the command-line file redirection operator (>).

What happens nil value is passed to puts?

Puts will produce an empty line if nil is input; nil is transformed into an empty string when represented as a string. Therefore, puts will output a blank line if nil is passed to it.

Conclusion 

The "puts" method in Ruby is one of Ruby's most essential tools for printing output. Its function is used to output strings and objects as readable text. It offers simplicity and flexibility in printing arguments on new lines.

We hope this blog has helped you enhance your knowledge of puts in Ruby. Do not stop learning! We recommend you read some of our articles related to puts in Ruby: 

 

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

We wish you Good Luck! 

Happy Learning!

Live masterclass