Whenever I look at an uniform wall of text, my brain freezes. I really have trouble parsing log files and similar types of output. For that reason, one of my favorite gemss is AmazingPrint
(which is a fork of AwesomePrint
).
What does it do? Well, it provides the ability to pretty print objects using a colored output … among other things.
Install and require the gem
Let's install it in order to try it out.
gem install amazing_print
And require it in our test file.
"amazing_print" require
The ap
command
The first method to note is ap
.
It lets us really pretty print an object. Let's start with a good old string.
"Hello, World" ap
If we run the file, we can see that our string was printed in yellow.
The same goes, for example, for an integer number.
123 ap
But this time the output is colored blue.
In fact, AmazingPrint
will print in several colors, depending on the type of object.
If we print a hash with several types as values
ap(
{symbol: :sybmol,
string: "string",
nil: nil,
true: true,
false: false,
integer: 1,
float: 2.3,
rational: Rational(4,5),
regex: /\A(\d){3}[ab]+-*$/i,
object: Object.new,
array: %w[one two three four five],
time: Time.now,
date: Date.today,
datetime: DateTime.now,
} )
we can see a rainbow of an output.
We can note as well that the hash keys and values are now beautifuly aligned.
The same goes for the array elements.
It also numbers the array elements in order, which is pretty cool
This makes it easier to parse by the human eye.
AmazingPrint
also gives us the ability to put color our string outputs.
It adds several methods named after colors to the String
class.
We can now call the red
or blue
method,
"This text is red".red
puts "This text is blue".blue puts
in order to print the strings in those colors respectively.
It also provides methods with the ish
suffix,
"This text is redish".red
puts "This text is redish".redish
puts "This text is blue".blue
puts "This text is blue".blueish puts
which will print the colors in a darker shade.
There's currently no way to get a list of all the available colors on the gem, but browsing though the source code I could identify them.
Here's the list.
%w[gray red green yellow blue purple cyan white].each { |color|
puts color.send(color)"#{color}ish".send("#{color}ish")
puts }
And it's corresponding output.
My configurations
AmazingPrint
's defaults are great, but I like to change some of them for my work.
To change the defaults, we just call the defaults=
method passing in a hash with the custom configuration we want. If you want to see all the available options, you can visit https://github.com/amazing-print/amazing_print#usage[AmazingPrint's page,role=external,window=blank].
AmazingPrint.defaults = {
}
In my case, I like to change only two settings.
First, I set the indent level to two spaces, which is what I like.
And second, I disable indexes for arrays, since they produce an output that can't be copied and pasted as Ruby code.
"amazing_print"
require AmazingPrint.defaults = {
indent: 2,
index: false,
}
Conclusion
I use AmazingPrint
in most of the projects I work on because I like to have the ability to format output as my heart desires as easily as possible.
I encourage you to try it out yourself and see if it serves you the same way it serves me.
In a future episode I'll show off some tips and tricks I use myself in my projects.
Thanks for watching and I'll see you in the next one.