puts in Ruby

Learn via video courses
Topics Covered

Overview

Ruby is a popular programming language known for its simplicity, readability, and flexibility. It offers a variety of methods to manipulate and display data on the console. One of the most commonly used methods for displaying output on the console is puts. In this article, we will explore the puts method in Ruby in detail, including its syntax, usage, and advantages over other printing methods.

Introduction

What is puts Method in Ruby?

puts is a built-in method in Ruby that is used to display the output of a program on the console. The method is short for put string and is used to print a string followed by a newline character. The syntax for using Ruby puts is:

Syntax of Ruby puts:

Output:

Explanation:

The above code will print the string Hello, World! on the console and add a newline character at the end. The puts method can also be used to display the value of a variable, array, or any other object.

Various Printing Methods in Ruby

Ruby offers several other printing methods apart from puts, including print and p. These methods display output on the console, but they differ in how they format the output.

Ruby print method:

The print method is similar to puts but does not add a newline character at the end of the output. For example:

Code:

Output:

Explanation:

The above code will print the string Hello, World! I want this on the same line on the console, which depicts that the print method does not add a newline character at the end of the output.

Ruby p method:

On the other hand, the p method is used to display an object's raw value. It is mainly used for debugging purposes as it displays the exact value of the object, including any quotes or escapes characters. For example:

Code:

Output:

Explanation:

The above code will print the string "Hello, World!" on the console, including the double quotes.

Difference Between puts, print, and p in Ruby

Now that we have seen the syntax and usage of the Ruby puts method, let us compare it with the other printing methods available in Ruby.

Ruby puts vs print

The main difference between puts and print is that puts adds a newline character at the end of the output, while print does not. Let us understand this with an example:

Code 1:

Output 1:

Code 2:

Output 2:

Explanation:

In the first code snippet (Code 1), puts is used to print Hello, World! followed by a new line character, and then print is used to print Hello, World! without a new line character. Therefore, the output is Hello, World! followed by a new line character on one line and Hello, World! on the next line without a new line character.

In the second code snippet (Code 2), print is used to print Hello, World! without a new line character, and then puts is used to print Hello, World! followed by a new line character. Therefore, the output is Hello, World!, and then Hello, World! on the same line with a new line character moving the cursor to the next line.

Multiple Arguments Case Handling:

Another difference between the two methods is how the two methods handle multiple arguments provided to them as input. Ruby puts automatically inserts a newline character between each argument provided as input. Conversely, the print method concatenates multiple arguments without any newline or space character, resulting in them being printed on the same line. For example:

Code:

Output:

Explanation: As you can see, puts adds a newline character after each argument, while print does not.

Arrays as arguments:

The puts method is useful when we want to print each element of an array on separate lines, while the print method is useful when we want to print the entire array as a single line of output.

Code:

Output:

Code:

Output:

Explanation:

When an array is passed as an argument to the Ruby puts method, the puts method automatically adds a newline character after each element of the array while printing it. Therefore, in the first code snippet, the puts method prints the elements of the array [10,20] on separate lines.

On the other hand, when an array is passed as an argument to the print method, the print method does not add any extra character while printing the array elements. Therefore, in the second code snippet, the print method prints the entire array [10,20] as a single line of output.

Ruby puts vs p

The main difference between Ruby puts and Ruby p is that the p method displays the raw value of an object, including any quotes or escape characters, while puts does not. Let us understand this with an example:

Code:

Output:

As you can see, puts interprets the escape character \n as a newline character and prints the two words on separate lines, while p displays the raw value of the string, including the escape character.

Relation between Ruby's p method and inspect method:

The p object in ruby is the same as puts object.inspect.

The .inspect method in Ruby returns a string representation of an object that closely resembles the code used to create the object. This makes it a useful tool for inspecting objects, as it provides a clear and accurate view of their state.

For example, if we call .inspect on the integer 5, it will return the string 5, which is the same as the original integer value. Similarly, calling inspect on a string like test string or an array like [10, 20, 30] will return a string representation of the original value.

However, when working with large amounts of code, it can become difficult to write puts object.inspect for every object we want to inspect. To simplify this process, Ruby provides the p method, which is defined as follows:

Ruby p method using .inspect method:

Using the p method, we can easily print the string representation of an object by passing it as an argument to the method. This saves us the trouble of writing out puts object.inspect every time we want to inspect an object.

Conclusion

  • In this article, we explored the Ruby puts method in detail, including its syntax, usage, and advantages over other printing methods.
  • We also compared puts with print and p and discussed their differences.
  • As a beginner, you can start with puts and print to display output on the console. As you become more comfortable with the language, you can start using p for debugging purposes.
  • Remember that choosing the right printing method can make your code more readable and efficient.