Created
June 7, 2014 14:07
-
-
Save shinokada/9fc20b365ea9c8bbeeb1 to your computer and use it in GitHub Desktop.
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
# http://blog.flatironschool.com/post/35154441787/rubys-each-with-object | |
hash = {} | |
['red', 'green', 'blue'].each do |color| | |
hash[color] = 'primary' | |
end | |
# Using inject, we don't have to initialize the hash ahead of time | |
['red', 'green', 'blue'].inject({}) do |hash, color| | |
hash[color] = 'primary' | |
hash | |
end | |
# by using each_with_object you don't need to return like hash at the end. | |
['red', 'green', 'blue'].each_with_object({}) do |hash, color| | |
hash[color] = 'primary' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment