• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Using Ruby to Print to Console

Using Ruby to Print to Console

October 14, 2022 Leave a Comment

There are 3 main ways we can use Ruby to print to the console. We can do this by using either the print, puts, or p commands. Let’s take a look at each one.

Let’s first look at printing to the console using the print command, with a very simple example.

some_string = "This is a string."
another_string = "This is another string."

print some_string
print another_string

#Output
This is a string.This is another string.

As you can see, the print command will print each string on the same line next to each other.

Let’s take a look at what the puts command will do.

some_string = "This is a string."
another_string = "This is another string."

puts some_string
puts another_string

#Output
This is a string.
This is another string.

As you can see, the puts command will return a newline after it prints, making it so each result is on its own line.

And finally, let’s take a look at what the p command will do.

some_string = "This is a string."
another_string = "This is another string."

p some_string
p another_string

#Output
"This is a string."
"This is another string."

The p command is similar to puts in that it will return a newline after it prints, making it so each result is on its own line. The p command is more for debugging your code, as it will help you know what value it is that is being printed. As in the p example above, you can see that we have a string object returned.

Let’s take a look at some more examples of each command in action.

some_string = "This is a string."

print some_string
puts some_string
p some_string

#Output
This is a string.This is a string.
"This is a string."

Let’s take a look at printing some numbers.

some_numbers = 123456

print some_numbers
puts some_numbers
p some_numbers

#Output
123456123456
123456

And printing an array of numbers.

some_array = [1,2,3,4,5,6]

print some_array
puts some_array
p some_array

#Output
[1, 2, 3, 4, 5, 6]1
2
3
4
5
6
[1, 2, 3, 4, 5, 6]

Here you can see how print and puts differ when printing an array.

Let’s try printing a Hash.

some_person = {"first_name" => "Peter", "last_name" => "Smith", "age" => "40"}

print some_person
puts some_person
p some_person

#Output
{"first_name"=>"Peter", "last_name"=>"Smith", "age"=>"40"}{"first_name"=>"Peter", "last_name"=>"Smith", "age"=>"40"}
{"first_name"=>"Peter", "last_name"=>"Smith", "age"=>"40"}

And finally, just printing a boolean value.

some_bool = true

print some_bool
puts some_bool
p some_bool

#Output
truetrue
true

Hopefully this article has been helpful for you to learn how to use Ruby to print to the console.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Convert String to Array
  • 2.  How to Square a Number in Ruby
  • 3.  Ruby String Comparison
  • 4.  How to Print Without Newline in Ruby
  • 5.  Ruby has_value Method – Check if Hash Has Value
  • 6.  Using Ruby to Capitalize First Letter of String
  • 7.  Ruby has_key Method – Check if Hash Has Key
  • 8.  Ruby unshift Method – Add Items to Start of Array
  • 9.  Get the Sum of an Array in Ruby
  • 10.  Ruby Ceiling – Find the Ceiling of a Number with the ceil Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy