DEV Community

Cover image for Print Smarter in Ruby - Know When and How to Use Each Method
Rob Race
Rob Race

Posted on • Originally published at robrace.dev

Print Smarter in Ruby - Know When and How to Use Each Method

If you've ever stared at a blob of console output wondering what the hell you just printed, you're not alone. Ruby gives you a handful of ways to print stuff, but knowing when to use which one is the real trick. In this post, we'll go through the five most common ways to print output in Ruby, plus one gem that makes your console look like it's had a glow-up.

puts: Standard Output with Automatic Newlines

puts is your go-to for dumping simple output to the console. It stands for "put string" and adds a newline at the end of each argument. It's as friendly and low-drama as it sounds:

puts "Hello, World!"
puts 123
puts ["Ruby", "Rails"]
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World!
123
Ruby
Rails
Enter fullscreen mode Exit fullscreen mode

Arrays get printed line-by-line, so don't be surprised when each item shows up on its own line.

print: Continuous Output Without Newlines

Want to print multiple things on the same line? Use print. It's like puts, but without the automatic newlines:

print "Hello, "
print "World!"
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

If you want a newline, you'll have to add it manually:

print "Hello, World!\n"
Enter fullscreen mode Exit fullscreen mode

p: Debugging with Inspect Output

p is the debug sibling of puts. It shows the inspect version of the object and means strings keep their quotes, and you get more detail:

p "Hello, World!"
p [1, 2, 3]
p({name: "Rob", age: 39})
Enter fullscreen mode Exit fullscreen mode

Output:

"Hello, World!"
[1, 2, 3]
{:name=>"Rob", :age=>39}
Enter fullscreen mode Exit fullscreen mode

If you're debugging and want raw visibility into what you're working with, p is your friend.

pp: Pretty Print for Enhanced Readability

For more complex stuff like nested hashes, pp (pretty print) makes things readable:

require 'pp'

complex_hash = {name: "Rob", hobbies: ["coding", "fitness", {sports: ["cycling", "lifting"]}]}
pp complex_hash
Enter fullscreen mode Exit fullscreen mode

Output:

{:name=>"Rob",
 :hobbies=>["coding", "fitness", {:sports=>["cycling", "lifting"]}]}
Enter fullscreen mode Exit fullscreen mode

Think of it like p, but with spatial awareness.

y: YAML Output in IRB

The y method is only available in IRB (Interactive Ruby), but it's a great way to spit out YAML-formatted data without much effort:

# In IRB
data = {name: "Rob", languages: ["Ruby", "JavaScript"]}
y data
Enter fullscreen mode Exit fullscreen mode

Output:

---
:name: Rob
:languages:
- Ruby
- JavaScript
Enter fullscreen mode Exit fullscreen mode

It's tidy, structured, and way more readable when you're dealing with nested stuff.

Quick Summary

Method When to Use
puts General output, automatic newline
print Continuous output without newline
p Debugging with inspect details
pp Pretty printing for readability
y YAML-formatted output (IRB only)

If you just want something quick and readable, puts and p will get you far. But if you're working with nested hashes from an API and don't want to burn your eyes out reading the raw dump then read on.

awesome_print: Enhanced Pretty Printing with Style

If pp gets the job done, awesome_print gets the job done with flair. It pretty-prints Ruby objects in full color, indents them beautifully, and makes your console output feel like VS Code decided to move in.

Installation

Using Bundler:

group :development do
  gem 'awesome_print'
end
Enter fullscreen mode Exit fullscreen mode

Then run:

bundle install
Enter fullscreen mode Exit fullscreen mode

Or install it directly:

gem install awesome_print
Enter fullscreen mode Exit fullscreen mode

Usage

require 'awesome_print'

data = {
  name: "Rob",
  hobbies: ["coding", "fitness", { sports: ["cycling", "lifting"] }]
}

ap data
Enter fullscreen mode Exit fullscreen mode

Output:

{
    :name => "Rob",
    :hobbies => [
        [0] "coding",
        [1] "fitness",
        [2] {
            :sports => [
                [0] "cycling",
                [1] "lifting"
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

To make it the default in IRB:

require 'awesome_print'
AwesomePrint.irb!
Enter fullscreen mode Exit fullscreen mode

Once you've used awesome_print, going back to puts feels like reading minified JSON with no line breaks.


That's your tour of Ruby's printing options. Use puts and p when you're starting out. Graduate to pp and awesome_print when your objects get gnarly. And next time you're knee-deep in nested hashes wondering why nothing looks right, don't forget your options.

Top comments (1)

Collapse
 
jamey_86 profile image
Jamey

Nice posting! Looking forward to talking to you soon