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.)
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?)
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)
If you don't have any fancy gem action, but do have JSON, this CLI line will work on a hash:
puts JSON.pretty_generate(my_hash).gsub(":", " =>")
#=>
{
:key1 => "value1",
:key2 => "value2",
:key3 => "value3"
}
Use the answers above if you're printing to users.
If you only want to print it for yourself in console, I suggest using the pry gem instead of irb. Besides pretty printing, pry has a lot of other features as well (check railscast below)
gem install pry
And check this railscast:
http://railscasts.com/episodes/280-pry-with-rails
Easy to do with json if you trust your keys to be sane:
JSON.pretty_generate(a: 1, 2 => 3, 3 => nil).
gsub(": null", ": nil").
gsub(/(^\s*)"([a-zA-Z][a-zA-Z\d_]*)":/, "\\1\\2:"). # "foo": 1 -> foo: 1
gsub(/(^\s*)(".*?"):/, "\\1\\2 =>") # "123": 1 -> "123" => 1
{
a: 1,
"2" => 3,
"3" => nil
}
If you need
inspect
on the objects in the Hash
inspect
method in your objects like you're supposed to... then this works great! (And gets better, the bigger and more nested your Hash object is.)
logger.error my_hash.pretty_inspect
For example:
class MyObject1
def inspect
"<#{'*' * 10} My Object 1 #{'*' * 10}>"
end
end
class MyObject2
def inspect
"<#{'*' * 10} My Object 2 #{'*' * 10}>"
end
end
my_hash = { a: 1, b: MyObject1.new, MyObject2.new => 3 }
Rails.logger.error my_hash
# {:a=>1, :b=><********** My Object 1 **********>, <********** My Object 2 **********>=>3}
# EW! ^
Rails.logger.error my_hash.pretty_inspect
# {:a=>1,
# :b=><********** My Object 1 **********>,
# <********** My Object 2 **********>=>3}
pretty_inspect
comes from PrettyPrint, which rails includes by default. So, no gems needed and no conversion to JSON needed.
If you're not in Rails or if the above fails for some reason, try using require "pp"
first. For example:
require "pp" # <-----------
class MyObject1
def inspect
"<#{'*' * 10} My Object 1 #{'*' * 10}>"
end
end
class MyObject2
def inspect
"<#{'*' * 10} My Object 2 #{'*' * 10}>"
end
end
my_hash = { a: 1, b: MyObject1.new, MyObject2.new => 3 }
puts my_hash
# {:a=>1, :b=><********** My Object 1 **********>, <********** My Object 2 **********>=>3}
# EW! ^
puts my_hash.pretty_inspect
# {:a=>1,
# :b=><********** My Object 1 **********>,
# <********** My Object 2 **********>=>3}
Big ol' pretty_inspect
ed Hash example from my project with project-specific text from my inspected objects redacted:
{<***::******************[**:****, ************************:****]********* * ****** ******************** **** :: *********** - *** ******* *********>=>
{:errors=>
["************ ************ ********** ***** ****** ******** ***** ****** ******** **** ********** **** ***** ***** ******* ******",
"************ ************ ********** ***** ****** ******** ***** ****** ******** **** ********** is invalid",
"************ ************ ********** ***** ****** ******** is invalid",
"************ ************ ********** is invalid",
"************ ************ is invalid",
"************ is invalid"],
:************=>
[{<***::**********[**:****, *************:**, ******************:*, ***********************:****] :: **** **** ****>=>
{:************=>
[{<***::***********[**:*****, *************:****, *******************:**]******* :: *** - ******* ***** - *>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: *** - *>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ********* - *>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ********** - ********** *>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ******** - *>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: **** - *******>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: *** - ********** ***** - *>=>
{}}]}},
{<***::**********[**:****, *************:**, ******************:*, ***********************:****] ******************** :: *** - *****>=>
{:errors=>
["************ ********** ***** ****** ******** ***** ****** ******** **** ********** **** ***** ***** ******* ******",
"************ ********** ***** ****** ******** ***** ****** ******** **** ********** is invalid",
"************ ********** ***** ****** ******** is invalid",
"************ ********** is invalid",
"************ is invalid"],
:************=>
[{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - ********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - ********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *******>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]*********** :: ****>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *******>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *******>=>
{:errors=>
["********** ***** ****** ******** ***** ****** ******** **** ********** **** ***** ***** ******* ******",
"********** ***** ****** ******** ***** ****** ******** **** ********** is invalid",
"********** ***** ****** ******** is invalid",
"********** is invalid"],
:**********************=>
[{<***::*******************[**:******, ************************:***]****-************ ******************** ***: * :: *** - ***** * ****** ** - ******* * **: *******>=>
{:errors=>
["***** ****** ******** **** ********** **** ***** ***** ******* ******",
"***** ****** ******** **** ********** is invalid"],
:***************=>
[{<***::********************************[**:******, *************:******, ***********:******, ***********:"************ ************"]** * *** * ****-******* * ******** * ********* ******************** *********************: ***** :: "**** *" -> "">=>
{:errors=>["**** ***** ***** ******* ******"],
:**********=>
{<***::*****************[**:******, ****************:["****** ***", "****** ***", "****** ****", "******* ***", "******* ****", "******* ***", "****"], **:""] :: "**** *" -> "">=>
{:errors=>
["***** ******* ******",
"***** ******* ******"]}}}}]}}]}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - ********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}}]}}]}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With