Created
June 8, 2012 10:39
-
-
Save decioferreira/2894942 to your computer and use it in GitHub Desktop.
Enumerable#each_with_object
This file contains 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
%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } | |
# => {'foo' => 'FOO', 'bar' => 'BAR'} |
This file contains 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
evens = (1..10).each_with_object([]) {|i, a| a << i*2 } | |
# => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment