In Rails
Se hai bisogno
- un hash "piuttosto stampato"
- ad esempio in Rails.logger
- che, in particolare, viene eseguito
inspect
sugli oggetti nell'hash
- che è utile se sostituisci / definisci il
inspect
metodo nei tuoi oggetti come dovresti
... allora funziona alla grande! (E migliora, più grande e nidificato è il tuo oggetto Hash.)
logger.error my_hash.pretty_inspect
Per esempio:
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
proviene da PrettyPrint , che include rotaie per impostazione predefinita. Quindi, non sono necessarie gemme e nessuna conversione in JSON necessaria.
Non in rotaie
Se non sei in Rails o se per qualche motivo non si verifica quanto sopra, prova a utilizzare require "pp"
prima. Per esempio:
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}
Un esempio completo
pretty_inspect
Esempio di Big ol ' ed Hash del mio progetto con testo specifico del progetto dai miei oggetti ispezionati redatto:
{<***::******************[**:****, ************************:****]********* * ****** ******************** **** :: *********** - *** ******* *********>=>
{: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=>
["***** ******* ******",
"***** ******* ******"]}}}}]}}]}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - ********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}}]}}]}}