Raylarda
Eğer ihtiyacın varsa
- "güzel baskılı" karma
- örneğin Rails.logger
- özellikle
inspect
Hash içindeki nesneler üzerinde
çalışan
inspect
nesnelerinizde olması gerektiği gibi yöntemi geçersiz kılar / tanımlarsanız yararlı olur .
... o zaman bu harika çalışıyor! (Ve daha iyi olur, Hash nesneniz daha büyük ve daha iç içe.)
logger.error my_hash.pretty_inspect
Örneğin:
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
raylar varsayılan olarak içeren PrettyPrint'ten gelir . Yani, hiçbir mücevher ve JSON dönüşüm gerekli.
Raylarda Değil
Rails'te değilseniz veya yukarıdaki herhangi bir nedenle başarısız olursa, require "pp"
önce kullanmayı deneyin . Örneğin:
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}
Tam Bir Örnek
pretty_inspect
Projemden, incelenen nesnelerimden projeye özgü metinlerle büyük ol ' ed Hash örneği düzeltildi:
{<***::******************[**:****, ************************:****]********* * ****** ******************** **** :: *********** - *** ******* *********>=>
{: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=>
["***** ******* ******",
"***** ******* ******"]}}}}]}}]}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:**]******* :: ****** - ** - *********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - ********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}},
{<***::***********[**:*****, *************:****, *******************:***]******* :: ****** - ** - **********>=>
{}}]}}]}}