Skip to content

Instantly share code, notes, and snippets.

@sranso
Created November 19, 2013 15:22
Show Gist options
  • Save sranso/7546968 to your computer and use it in GitHub Desktop.
Save sranso/7546968 to your computer and use it in GitHub Desktop.
.each vs .collect
[1, 2, 3, 4].each { |x| x*2 }
#=> [1, 2, 3, 4]
[1, 2, 3, 4].each { |x| print x*2 }
#=> 2468 => [1, 2, 3, 4]
[1, 2, 3, 4].collect { |x| x*2 }
#=> [2, 4, 6, 8]
[1, 2, 3, 4].each { |x| x*2 }
#=> 2468 [nil, nil, nil, nil]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment