Created
March 4, 2014 17:34
-
-
Save carlossanchezp/9351495 to your computer and use it in GitHub Desktop.
for vs each
This file contains hidden or 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
| 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