Sam Fitz7,036 Points
The Distinction Between For Loops and Other Kinds of Loops
Near the end of this video (2:55), Jason says that the variables for For Loops exist outside of the loop, but for Other types of Loops the variables exist only inside the loop.
Can someone please clarify his meaning here? It seems to be a significant point, but I feel I don't understand. The videos and challenges I've completed up to this point demonstrate that "Loop ", "While", "Each", and "If" loops all can use variables outside of the statement of loop itself.
Maciej Czuchnowski36,427 Points
That is an interesting concept. So here is an example of an .each loop (they are preferred in Ruby and Rails):
[1, 2, 3, 4, 5].each do |a| # do something here end puts a
This code will fail and the error will say "undefined local variable or method 'a' for main:Object (NameError)". Why? Because variable
a was created inside the loop and only exists between its
end keywords. Outside that scope it does not exist in this particular example. You may define variable
a before the loop and then there will be no error. Example:
a = 0 [1, 2, 3, 4, 5].each do |a| # do something here end puts a
Now this would print
0 on the screen. Why? Because
a exists outside the loop and has value 0. Inside the loop it is shadowed by a temporary local variable of the same name. Once the loop ends, the only available variable
a is the one that had the value 0.
for loop has a different behavior. If you take this code:
for a in 1..5 # do something here end puts a
You might expect this to cause the same error as before, since this is almost the exact same code as the first snippet. But it prints out 5 without any errors or warnings. That is because the
a variable, although it was created inside the loop and should be local for it, exists outside this scope between
end. And if you had the variable
a defined before like this:
i = 0 for i in 1..5 # do something here end puts i
This will print 5, not 0 like in the second example. That is because shadowing did not take place - the for loop was operating on an existing variable, did not create its own local loop to work with.
This is why Ruby programmers tend to avoid using the
for loop in favor of
each loop unless they want this very behavior.
Thank you for the examples, that was very helpful!
Izaäk Louwerse1,697 Points
I recommend this link, also as explanation: https://stackoverflow.com/questions/5677081/ruby-what-is-the-difference-between-a-for-loop-and-an-each-loop