require 'pp'
February 13th, 2007
It wasn’t long into my use of ./script/console that I desired a prettier print method. As a gdb user, I was used to ‘set print pretty on’. I spent a few minutes trying to figure out how to do the same in console/irb and failed and moved on. Today I was thumbing through my new copy of PickAxe rev. 2 and stumbled into ‘pp’. Let’s look at the difference:
>> p Task.find(:first)
#<Task:0x314474c @attributes={"job_id"=>"2", "id"=>"1", "description"=>"fill out the unit tests for the existing model and controllers", "duration"=>"80", "started_at"=>"2007-02-02"}>
#<Task:0x314474c @attributes={"job_id"=>"2", "id"=>"1", "description"=>"fill out the unit tests for the existing model and controllers", "duration"=>"80", "started_at"=>"2007-02-02"}>
Not so pretty. Let’s try pp:
>> require ‘pp’
=> ["PP"]
>> pp Task.find(:first)
#<Task:0x3150ba0
@attributes=
{"job_id"=>"2",
"id"=>"1",
"description"=>
"fill out the unit tests for the existing model and controllers",
"duration"=>"80",
"started_at"=>"2007-02-02"}>
=> ["PP"]
>> pp Task.find(:first)
#<Task:0x3150ba0
@attributes=
{"job_id"=>"2",
"id"=>"1",
"description"=>
"fill out the unit tests for the existing model and controllers",
"duration"=>"80",
"started_at"=>"2007-02-02"}>
Much better!
update: Colors provided by SubEthaEdit’s fancy “Copy as XHTML”.