I have a hash which has many hash inside and the value may be an array and this array consist of many hashes, I wanted to print all key value pair, If values is array, then it has to print
"pageOfResults": "Array" # I don't want actual array here, I want the string "Array" to be printed.
Of If hash follows, then it needs to print
"PolicyPayment": "Hash"
else It needs to print key and value
Key -> "CurrencyCode":
Value-> "SGD",
Hash follows
a={
"PageOfResults": [
{
"CurrencyCode": "SGD",
"IpAddress": nil,
"InsuranceApplicationId": 6314,
"PolicyNumber": "SL10032268",
"PolicyPayment": {
"PolicyPaymentId": 2188,
"PaymentMethod": "GIRO"
},
"InsuranceProductDiscountDetail": nil,
"ProductDetails": {
"Results": [
{
"Id": 8113,
"InsuranceProductId": 382,
"ApplicationProductSelectedId": 62043,
},
"InsuranceProduct": {
"InsuranceProductId": 382,
"ProductCode": "TL70-90",
},
],
},
}
]
}
Program I have written to print these values
a.each do |key,value|
puts "key->" + key.to_s
if value.is_a?Array
value.each do |v|
v.each do |key,value|
puts "key->"+key.to_s
puts "value->"+value.to_s
end
end
else
puts "value->"+value.to_s
end
This program is printing first of level of hash and it's values, I can do recursive calls to print all values as well,
But my question is, Is there any way we can write Ruby style of code to accomplish this easily? Or any better way?