Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created June 7, 2014 14:07
Show Gist options
  • Save shinokada/9fc20b365ea9c8bbeeb1 to your computer and use it in GitHub Desktop.
Save shinokada/9fc20b365ea9c8bbeeb1 to your computer and use it in GitHub Desktop.
# 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