I’m doing a whole bunch of stuff with an array of hashes, and learning a ton.
I am trying to loop through the array and change some of the values, and remove some, etc. etc.
I think that because I’m using things like
find_best = original_array.select{|h| h[:score]>15}
I’m ending up with a new array of the matches, rather than a bunch of pointers to the original array.
So when I say (as example)
find_best.each do |something|
#manipulate somehow
end
Those changes are not reflected in the original_array.
I guess I could merge the find_best into the original_array, though I’m not entirely sure how to do that, or if that is the right thing to do.
The biggest problem I run into is when I say
while find_best.length>0
#do some stuff that recalculates find_best
find_best = original_array.select{|h| h[:score]>15} #so I'm resetting the loop for find_best and exit if all the work is done.
end
As Niklas already noted in the comment,
select,mapand alike create new arrays, but they contain the very objects from the original array, not their copies.Observe:
See? A member of the original array was modified.