Skip to content

Instantly share code, notes, and snippets.

@ogawaso
Created March 27, 2012 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ogawaso/2211765 to your computer and use it in GitHub Desktop.
Save ogawaso/2211765 to your computer and use it in GitHub Desktop.
difference between for and each in ruby
# -*- coding: utf-8 -*-
a= [1, 2, 3]
for v in a
b = 'a'
puts v
end
puts b
a.each do |v|
c = 'a'
puts v
end
puts c
------------------------------
1
2
3
a
1
2
3
hoge.rb:12: undefined local variable or method `c' for main:Object (NameError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment