I am looping through a list of words, and am inserting each letter of the alphabet at the beginning of each word, with this code:
def add_charac_front
("a".."z").each do |letter|
@array.each do |list_word|
list_word.insert(0, letter)
puts list_word
end #ends @array loop
end #ends alphabet loop
end #ends method
but .insert is changing @array so that when I loop through @array for the letter "b", the first list_word in @array is not "Hello" but "aHello".
I need the exact same behavior, but for @array to be the same array for each letter loop I run. It is working correctly when I do this code:
def add_charac_front
("a".."z").each do |letter|
@array.each do |list_word|
puts "#{letter}#{list_word}"
end #ends @array loop
end #ends alphabet loop
end #ends method
But I eventually want to insert letters in different parts of the list_word, not just the front.
I guess the other way I could do this is to .split("") the list_word, then .insert(0, letter), then .join. But it seems way more cumbersome.
How do I do this?
list_word.insert(0, letter)the 0 is indicating the 0 index (in other words 'position'), whereas I could put 3 to insert the letter into the 3 index.