Knight's Travails question


#1

Is there a way to iterate over a hash while adding to it? I keep getting a runtime error from:

hash.each do |key, val|
exit_meth if key == target_val

array(sub_arr).each do |sub_arr|
    hash[sub_arr] = key
end

end

I tried iterating over the hash.keys instead but that stops after iterating the initial values of hash.


#2

You could probably use an Enumerable method like each_with_object to create a new hash with the results you are looking for, so that you aren’t mutating your original hash.


#3

each_with_object seems more like a one and done type deal. What I intend is to take a key from the hash, generate some new moves/data from said key, and append those new moves/data back into the hash before going to the very next hash key. I don’t know beforehand what squares the hash will contain or when a match will be found which will stop the adding of new hash keys.