jeudi 22 décembre 2016

I am not able to format the string to get desired result

I have a method that return string in the view. I am not able the format the string to desired output.

return_string = ""
test = {
      "1"=>{:name=>"joe", :age=>"1", :model=>"ABCD-000D4"}, 
      "2"=>{:name=>"Dav", :age=>"2", :model=>"EFGH-000D3"}, 
      "5"=>{:name=>"Pok", :age=>"5", :model=>"EFGH-000D3"}, 
      "4"=>{:name=>"Lan", :age=>"4", :model=>"EFGH-000D3"}, 
      "3"=>{:name=>"Bas", :age=>"3", :model=>"EFGH-000D3"},
      "6"=>{:name=>"Kit", :age=>"6", :model=>"ABCD-000D4"},
      "7"=>{:name=>"Sop", :age=>"7", :model=>"ABCD-000D4"}
       }
test.keys.each do |age|
  return_string += "AGE: #{age} = <b><font color='blue'>Model: #{test[age][:model]}</font></b><br/>"
end

Output I am getting now:

AGE: 1 = Model: ABCD-000D4
AGE: 2 = Model: EFGH-000D3
AGE: 5 = Model: EFGH-000D3
AGE: 4 = Model: EFGH-000D3
AGE: 3 = Model: EFGH-000D3
AGE: 6 = Model: ABCD-000D4
AGE: 7 = Model: ABCD-000D4

Desired Output

AGE: 1 = Model: ABCD-000D4
AGE: 2-5 = Model: EFGH-000D3
AGE: 6-7 = Model: EFGH-000D4

Aucun commentaire:

Enregistrer un commentaire