Print in Ruby

Learn via video courses
Topics Covered

:::

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

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Print

  • 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.