Best way to pretty print a hash

I have a large hash with nested arrays and hashes. I would like to simply print it out so it 'readable' to the user.

I would like it to be sort of like to_yaml - that's pretty readable - but still too tech looking.

Ultimately its going to be end users who need to read these data chunks so they need to be formatted cleanly.

Any suggestions?


Solution 1:

require 'pp'
pp my_hash

Use pp if you need a built-in solution and just want reasonable line breaks.

Use awesome_print if you can install a gem. (Depending on your users, you may wish to use the index:false option to turn off displaying array indices.)

Solution 2:

If you have JSON, I recommend JSON.pretty_generate(hash) because it is simpler than awesome_print, looks great in a pre tag, and allows for easy copying from a web page. (See also: How can I "pretty" format my JSON output in Ruby on Rails?)

Solution 3:

Another solution which works better for me than pp or awesome_print:

require 'pry' # must install the gem... but you ALWAYS want pry installed anyways
Pry::ColorPrinter.pp(obj)