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 trial

Ruby Ruby Loops Ruby Iteration The For Loop

Jitendra Bansal
Jitendra Bansal
9,805 Points

The 'for' loop was not used

I am using the code attached. I can't see any error in the code. It just says the 'for' loop was not used. Help!

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

for i in 0..3 do
  puts animals[i]
  i+= 1
end

1 Answer

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

for i in animals
    puts i
end

Thats the Syntax for the for Loop in Ruby.

i is a dynamically declared variable that stores the value in the Array positions -> a string in this case in animals Points to the Array the Loop has to go through

All together it mean: go through all the positions in the animals Array and with every jump ( every index it Encounters) store that value to the i variable ( the value changes with every jump, because on every jump it goes 1 index further). And you have to use that variable in the Loop or you loose the value.

Hope this helped - > here's a LINK of all the Loops in Ruby and how their Syntax.