Guest User

transform hash into array

a guest
Feb 25th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.38 KB | None | 0 0
  1. >> test = { 1=>25, 2=>30, 5=>100 }
  2. => {1=>25, 2=>30, 5=>100}
  3. >> test2 = []
  4. => []
  5. >> test.each { |key,value| test2.push({ "device"=> key.to_s, "diskImage" => { "capacity" => value.to_s } }) }
  6. => {1=>25, 2=>30, 5=>100}
  7. >> test2
  8. => [{"device"=>"1", "diskImage"=>{"capacity"=>"25"}}, {"device"=>"2", "diskImage"=>{"capacity"=>"30"}}, {"device"=>"5", "diskImage"=>{"capacity"=>"100"}}]
Advertisement
Add Comment
Please, Sign In to add comment