My situation is quite sophisticated, that's why I replaced original data with simple numbers. So, please, don't pay attention to very simple data and 'idiotic' if conditions. It's just an example. Also, please, ignore typos if there's any - original code has no typos.
I have array of hashes with elements like my_hsh = {"numbers" => [1, 2, 3, 4], "n_count" => 4}
What I need to do:
- Loop through the parent array and select appropriate hashes,
- add each such hash to the array my_arr_nochange,
- loop through "numbers" in each such hash and add'em to the_hash["numbers"],
- add hashes without these numbers to my_arr_updt.
So the code:
the_hash = {"numbers" => []}
my_arr_updt = []
my_arr_nochange = []
array_of_hashes.each do |my_hsh|
  if my_hsh["n_count"] == 4
    my_arr_nochange << my_hsh
    updated_hsh = my_hsh
    my_hsh["numbers"].each do |num|
      if num == 2
        the_hash["numbers"] += [ num ]
        updated_hsh["numbers"] -= [ num ]
      end
    end
    my_arr_updt << updated_hsh
  end
end
return the_hash, my_arr_updt, my_arr_nochange
The problem is my_arr_nochange is being modified, so instead of getting the old state of my_hsh inside I get the new one. Like:
my_arr_updt
=> [{"numbers" => [1, 3, 4], "n_count" => 4}]
my_arr_nochange 
=> [{"numbers" => [1, 3, 4], "n_count" => 4}]
Tried splitting in different methods and using sub-variables. No result.
P.S.: If you could help with more appropriate title, I would appreciate that too.
 
    