Pretty Print a Really Big Hash in Ruby

Problem: Copy and paste a really big Ruby object so that it's human readable

Today I was building a fixture in one of my RSpec tests which consisted of a really big hash. It looked something like this:

really_big_hash =     {:Profile=>
                          :CreditUnionName=>"CITIZENS EQUITY FIRST",
                          :PeerGroup=>"6 - $500,000,000 and greater",
                          :CertificationDate=>"1/22/2016 10:46:28 AM",

When I tried to copy this fixture into my test file, I initially did this in console:

require 'clipboard' # use the Clipboard gem

The problem was that when I pasted the text it all ended up on one line like this:

{:Profile=>{:CharterNumber=>"68448", :CreditUnionId=>"14241", :CreditUnionName=>"CITIZENS EQUITY FIRST", :CycleDate=>...

Solution: Ruby's 'pp' module

I discovered that the Ruby 'pp' module could be used to format a Hash as a multiline output and I could capture the output. Here's what I did:

require 'pp'
cap =
$stdout = cap
$stdout = STDOUT


I was then able to paste my Hash as a more human-readable multi-line string. Note that because pp prints to screen by default, I had to reroute $stdout temporarily to capture the output via a StringIO object.