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

Nejc Vukovic
Nejc Vukovic
Full Stack JavaScript Techdegree Student 35,752 Points
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.