輸入紅寶石轉化哈希
i = { :Narration=>"RI journal",
:Status=>"DRAFT",
:JournalLines=>{
:JournalLine=>[
{ :credit=>{
:LineAmount=>"10",
:AccountCode=>"111",
:Description=>"check cred"
},
:debit=>{
:LineAmount=>"-10",
:AccountCode=>"222"
}
},
{ :credit=>{
:LineAmount=>"10",
:AccountCode=>"333"
},
:debit=>{
:LineAmount=>"-10",
:AccountCode=>"444"
}
}
]
}
}
而是「信用」,「借記卡」的,我想在一個數組簡單的值。
預期輸出:
{ :Narration=>"RI journal",
:Status=>"DRAFT",
:JournalLines=>
{ :JournalLine=>[
{:LineAmount=>"10", :AccountCode=>"111", :Description=>"check cred"},
{:LineAmount=>"-10", :AccountCode=>"222"},
{:LineAmount=>"10", :AccountCode=>"333"},
{:LineAmount=>"-10", :AccountCode=>"444"}
]
}
}
這是我的代碼,它的工作原理。
arr = []
i[:JournalLines][:JournalLine].each do |h|
h.each do |k, v|
arr << v
end
end
是否有更好的方法來轉換輸入替換兩個每個循環。