Ruby Ruby Loops Ruby Iteration The For Loop

Stuck on Ruby for loop. Why won't this run?

Just curious why my code isn't running. Do I need .each?

for.rb
animals = ["dog", "cat", "horse", "goat"]

for animals do |x|
  puts "#{x}"
end

4 Answers

I got it to work with this:

for i in animals do puts i end

Michael Feuerborn
Michael Feuerborn
5,504 Points

You will want the .each in order to tell the system to loop through the items I believe.

When I change it to:

for animals.each do |x| I then get this error:

SyntaxError: e44c53dd-eb17-42e9-aa06-1533d8550b14.rb:21: syntax error, unexpected end-of-input, expecting &. or :: or '[' or '.'

What unexpected end of input? All the rest of the code remained the same.

Michael Feuerborn
Michael Feuerborn
5,504 Points

If you remove the for in the block it will work. Calling each on that is all you should need :D

so

animals = ["dog", "cat", "horse", "goat"]

animals.each do |x| puts "#{x}" end

But it needs to be a 'for' loop... That's the exercise.

Michael Feuerborn
Michael Feuerborn
5,504 Points

You are fantastic! Well done!

Sorry I didn't see the for loop requirement >.<