Imagine: you open a rails console and type User.limit(100)
. The output shows your database call, then… ACTIVE RECORD GUT PUNCH!!! Your screen is overrun with an inscrutable wall of text.
I hated it. I had some coping mechanisms, but they were cumbersome. For example, the map-and-print:
puts User.all.map { |u| "#{u.id}, #{u.email}, #{u.bio}" }.join("\n"); 0
This still sucked. Tons of typing, mid-line typos, forgetting to start with ‘puts’, accidentally returning the whole array - and the output was better but still not great. “Why?!?” I cried. “When I use the SQL command line everything lines up!” The lightbulb flashed and table_print was born:
> tp User.all
ID | EMAIL | NAME | BIO
----------------------------------------------------------------------------------
1 | nikola.tesla@gmail.com | Nikola Tesla | I started working in teleph...
2 | john.singer@hotmail.com | John Singer | Born in New York City to Ir...
3 | charlottecooper@yahoo.com | Charlotte Cooper | I like tennis!
But wait… there’s more.
The most powerful feature of table_print is the ability to see data across tables. Here we investigate the orders of some customers – using strings to specify the method chains to traverse the ActiveRecord associations:
> tp User.limit(3), "name", "credit_cards.display_name", "credit_cards.orders.summary"
NAME | CREDIT_CARDS.DISPLAY_NAME | CREDIT_CARDS.ORDERS.SUMMARY
-------------------------------------------------------------------------------
Nikola Tesla | MC xxxxxxxx3938 | $12.19 for Switch Set 100pc
| | $98.00 for Wire Spool, 1/8-in...
| VISA xxxxxxxx4726 |
| AMEX xxxxxxxx9278 | $23.95 for Aluminum U-tube 12pk
| | $54.20 for Soldering Iron and...
John Singer | |
Charlotte Cooper | VISA xxxxxxxx9183 | $19.99 for Wilson Tennis Stri...
| | $279.00 for HEAD Graphene Rac...
| | $1.99 for Tourna Lead Tape
| MC xxxxxxxx5811 | $23.95 for Visor
| | $59.95 for Adidas Shoes, Size...
BOOOOOOOM!!!!!!!!!!!! I can’t tell you how many times that has saved my bacon during a production firedrill. Don’t settle for looking at tables individually, trying to relate one set of objects back to another, when you can table_print everything at once in context.
Get table_print at rubygems.org and check out the github README for full documentation.
Perhaps you’re wondering how this is different from some of your favorite IRB tools. Let’s take ‘em one by one.
First, awesome_print. It is awesome, but it’s geared toward inspecting individual objects or small groups of objects. Its indentation and coloring help you see what’s going on in your object hierarchy, but puts the values in a column far apart. table_print makes it easy to scan a column across groups of objects and notice patterns.
Second, hirb. It’s a “view framework” that lets you navigate through your objects. It’s geared toward browsing through a lot of data without much typing, and it supports a ton of different formats. But that comes with some heavy overhead around customization, and I have a difficult time creating the views I want to see – especially since what I want to see is constantly changing. table_print makes it easy to specify exactly what you want to see.
Get table_print at rubygems.org and check out the github README for full documentation.