In the following code, I am trying to get the indices of the occurrences of the letter guess in array secret_word, store the indices in an array indices, then, using indices, insert the same letter into another array user_word without disturbing the other letters that might already be in user_word.
if secret_word.include?(guess) #secret_word is an array of chars. guess is a char.
indices = Array.new
indices<< secret_word.each_index.select{ |letter| secret_word[letter] == guess } #verified that this array fills correctly
indices.each do |e|
user_word[e] = guess
end
end
The error message implies that each element of indices is an array, not a fixnum as expected. It will not let me use the element from indices to index into user_word. Help?
.each_indexwould yield0,1, ... up to the length of the array minus 1). A Hash would be referenced using objects. Issecret_wordreally an Array?