Skip to content

Instantly share code, notes, and snippets.

@gstark
Created June 22, 2011 13:35
Show Gist options
  • Save gstark/1040098 to your computer and use it in GitHub Desktop.
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
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)
@ambethia
Copy link

I found this out by looking at some commits where the author was changing for loops to Array#each, and I wondered what the distinction was; "The moar u know!".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment