Skip to content

Instantly share code, notes, and snippets.

@carlossanchezp
Created March 4, 2014 17:34
Show Gist options
  • Select an option

  • Save carlossanchezp/9351495 to your computer and use it in GitHub Desktop.

Select an option

Save carlossanchezp/9351495 to your computer and use it in GitHub Desktop.
for vs each
results = [ ]
for i in 1..3
results << lambda { i }
end
puts results.map { |l| l.call }
results = [ ]
(1..3).each do |i|
results << lambda { i }
end
puts results.map { |l| l.call }
list = %w{ google yahoo bing duckduckgo }
for engine in list
# do something with engine
end
puts engine # duckduckgo
list = %w{ google yahoo bing duckduckgo }
list.each do |engine|
# do something with engine
end
puts engine # NameError: undefined local variable or method ‘engine’
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment