Reputation: 41
i want to save objects into an array. I have JSON objects and I want to save every object in an array to access every element alone. Can anybody help me?
toArray = JSON.parse(res.body)
categ = Array.new
i = 0
toArray.each do |object|
newMyObject = MyObject.new(object)
categ = Array.new(i, newMyObject)
i = i+1
end
Upvotes: 0
Views: 157
Reputation: 30056
Try this one
array_from_json = JSON.parse(res.body)
objects_array = array_from_json.map { |item| MyObject.new(item) }
The issue in your code is that you are creating a new array every iteration.
Upvotes: 2