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 gems is 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 awesome_print
And require it in our test file.
"awesome_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, AwesomePrint
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,
} )
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.
AwesomePrint
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={{{shot(15)}}} 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.
Unfortunately, there's not a good way to instrospect the gem to find out what are the available colors. To find them, we can list all of the methods for the String
class and select which ones are colors:
"a string".methods.sort
# => [:!,
# :!=,
# ...
# :awesome_inspect,
# :awesome_print,
# :between?,
# :black,
# :blue,
# :blueish,
# :byteindex,
# ...
# :valid_encoding?,
# :white,
# :whiteish,
# :yellow,
# :yellowish,
# :yield_self]
Here's the curated list of colors.
%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.
AwesomePrint
'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 AwesomePrint's page.
"awesome_print"
require
AwesomePrint.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.
"awesome_print"
require
AwesomePrint.defaults = {
indent: 2,
index: false,
}
Conclusion
I use AwesomePrint
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.