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 123puts ["Ruby", "Rails"]
Output:
Hello, World!123RubyRails
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!"
Output:
Hello, World!
If you want a newline, you’ll have to add it manually:
print "Hello, World!\n"
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})
Output:
"Hello, World!"[1, 2, 3]{:name=>"Rob", :age=>39}
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
Output:
{:name=>"Rob", :hobbies=>["coding", "fitness", {:sports=>["cycling", "lifting"]}]}
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 IRBdata = {name: "Rob", languages: ["Ruby", "JavaScript"]}y data
Output:
---:name: Rob:languages:- Ruby- JavaScript
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
Then run:
bundle install
Or install it directly:
gem install awesome_print
Usage
require 'awesome_print'
data = { name: "Rob", hobbies: ["coding", "fitness", { sports: ["cycling", "lifting"] }]}
ap data
Output:
{ :name => "Rob", :hobbies => [ [0] "coding", [1] "fitness", [2] { :sports => [ [0] "cycling", [1] "lifting" ] } ]}
To make it the default in IRB:
require 'awesome_print'AwesomePrint.irb!
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.