Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMonroe Mann
3,273 PointsStuck on Ruby for loop. Why won't this run?
Just curious why my code isn't running. Do I need .each?
animals = ["dog", "cat", "horse", "goat"]
for animals do |x|
puts "#{x}"
end
4 Answers
Monroe Mann
3,273 PointsI got it to work with this:
for i in animals do puts i end
Michael Feuerborn
7,110 PointsYou will want the .each in order to tell the system to loop through the items I believe.
Monroe Mann
3,273 PointsWhen 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
7,110 PointsIf 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
Monroe Mann
3,273 PointsBut it needs to be a 'for' loop... That's the exercise.
Michael Feuerborn
7,110 PointsYou are fantastic! Well done!
Sorry I didn't see the for loop requirement >.<