Print in Ruby
:::
Overview
The Ruby print method is used to display output on the console screen. It is a built-in method in Ruby. It outputs the supplied arguments to the console screen without adding a new line at the end of the output. The print method, along with its syntax, parameters, return values, and examples, will be thoroughly examined in this article. We'll also compare the print in Ruby with other similar Ruby output methods.
What is Print in Ruby?
The print in Ruby method is a built-in method for displaying output on the console screen. It accepts one or more parameters and shows them in the console without including a newline character. The print method can take strings, numbers, arrays, and other objects as arguments.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Syntax
The basic syntax for print is:
Here, argument1, argument2, argument3, and so on are the arguments that we want to pass to the print method.
Parameters
The print in Ruby can take multiple arguments separated by commas. These arguments can be of any type, including strings, integers, and objects. The print method concatenates all the arguments and displays them on the console without adding a new line character at the end. If we pass multiple arguments, the print method inserts a space between each argument.
Return Values
The print in Ruby does not return any value explicitly. Instead, it returns nil implicitly after printing the output to the console.
Examples
Let's take a look at some examples to understand how the print in Ruby works:
Example 1:
In the above example, we have used the print method to display two separate strings without adding a new line character. The output is a concatenated string of both arguments.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example 2:
In the above example, we have used the print method to display a string and an integer value without adding a new line character. The output is a concatenated string of both arguments with a space between them.
Example 3:
In the above example, we passed an array of characters as an argument to the print method. It displays the array without adding a new line character.
Print vs Puts vs P in Ruby
Ruby has three built-in methods that are used for displaying output on the console screen. These methods are print, puts, and p.
Let's look at the difference between them:
Turn Learning into Career Growth
- The print method in Ruby prints the result to the console without including a newline character.
- It may concatenate several arguments separated by commas to generate a single output string.
- After showing the output, it implicitly returns nil.
Puts
- The puts method in Ruby prints the output to the console, including a newline character at the conclusion.
- It also can accept any number of parameters and concatenates them into a single string before printing them to the console screen
P
- The p method in Ruby prints the output to the console, including the quotes and escape characters.
- It also inserts a newline character at the end of the output, as the puts method does.
- It just returns the argument that was supplied to it.
In summary, the print method is used to display output without adding a newline character, while puts and p methods are used to display output with a newline character. The p method displays the output with quotes and escapes characters, while puts and print do not.
FAQs
Q: Can we use variables as arguments in the print method?
A: Yes, we can pass variables as arguments in the print method. The print method will concatenate all the arguments and if there is a variable then the value stored in the variable gets concatenated.
Q: What is the maximum number of arguments that we can pass to the print method?
A: There is no limit to the number of arguments that we can pass to the print method. We can pass as many arguments as we want, separated by commas.
Q: What happens if we pass a nil value as an argument to the print method?
A: If we pass a nil value as an argument to the print method, it will display nothing on the console. We will not get an error, but the output will be blank.
Conclusion
- Ruby has a built-in print method that can be used to display output on the console screen without a newline character.
- Any form of parameter, including texts, numbers, arrays, and objects, can be used numerous times.
- All of the arguments are concatenated by the print method, which then outputs them on the console screen.
- The print in Ruby can be used to concatenate many strings into a single string or display output on the same line.
- The output formatting differs from the puts and p methods but is similar.
- The print method is a powerful tool for presenting output in Ruby programming in a clear and readable way.




