Created
June 22, 2011 13:35
-
-
Save gstark/1040098 to your computer and use it in GitHub Desktop.
Difference between scope of block yield variable between for-in and Enumerable#each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [1,2,3,4] | |
array.each do |array_iter| | |
# do something with array_iter | |
end | |
# Hmm, array_iter isn't defined | |
puts defined?(array_iter) | |
for for_iter in array do | |
# do something with for_iter | |
end | |
# Hmm, for_iter *IS* defined | |
puts defined?(for_iter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this out by looking at some commits where the author was changing
for
loops toArray#each
, and I wondered what the distinction was; "The moar u know!".