How to create a Hash that includes a dynamically build array of hashes
I need to create something like this:
"paxReference" => [{
"ptc" => "ADT",
"traveller" => [
{
"ref" => "1"
},
{
"ref" => "2"
}
]
},
{
"ptc" => "CH",
"traveller" => [
{
"ref" => "3",
},
{
"ref" => "4",
}
]
},
]
But the array of hashes "traveller" must be built dynamically, I mean, If a pararmeter ADT_number is 4, I must include 4
"ref" => "1"
"ref" => "2"
"ref" => "3"
"ref" => "4"
Same thing for the "CH" segment.
How can I do this in Ruby?
The out put should look like this:
<paxReference>
<ptc>ADT</ptc>
<traveller>
<ref>1</ref>
</traveller>
<traveller>
<ref>2</ref>
</traveller>
</paxReferenc开发者_StackOverflowe>
<paxReference>
<ptc>CH</ptc>
<traveller>
<ref>3</ref>
</traveller>
<traveller>
<ref>4</ref>
</traveller>
</paxReference>
paxReference = ptcs.map do |ptc|
out_hash = {}
out_hash["ptc"] = ptc
out_hash["traveler"] = ADT_number.times.map {|i| {"ref" => i }}
end
This is in the case that ptcs is an array, and that ADT_number doesn't change for each one. If ADT_number changes, then you'll want to grab something with an index or something like that.
精彩评论