Ruby while loop and array problem (urgent!)


Write an example of a while loop that iterates through each element of an array called animals and turns each element into the string "turtle".
This is what I have so far in my code but it's not in a while loop format and it doesn't work. Can you please help?

animals = ["cat", "dog", "horse", "turtle"]
animals.each do


Please avoid terms like urgent or asap. Saying that it's urgent for you doesn't make it so for anyone else, and the suggestion that it would is a bit rude.

Is this something you can google? Without knowing what's stopping you, and because there are many examples of while-loops and arrays - I'm left with the conclusion that you're either leaving out information in your question or the information is already easily available to you. Either way I don't have an answer for you.

Maybe all you needed to know is that this is something you can easily search for, but I can't tell for sure what you're missing.


I've already tried to google as much as I can but I still can't solve it... I did not leave out information - I literally copied and pasted the instructions. I've changed my code to this, but it still doesn't work:
animals = ["cat", "dog", "horse", "turtle"]
index = 0
while index < animals.length
animals[index].each do


What needs to happen for your idea to work out? Are you for example computing the indexes that you want? Which ones do you want? Which ones do you currently get? How would you get the ones you want, what actions need to be carried out to get those?

You'd start by deciding on what should happen all the way through, then implement that, and if it's not having the effect you imagined then you'd need to start investigating where the code behaves differently from what you wanted


Well I wrote in my start of the topic what I am trying to do. I need to use a while loop to iterate through every element of an array, (I am guessing using .each) which is called animals and turns each element into the string "turtle". (I believe I should use .replace for that. )
The index is showing that for each object in the array, I will replace each of those with "turtle".


Nevermind. I solved the problem. Thanks for your time!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.