Created
December 28, 2020 23:28
-
-
Save dsasse07/4680d84d5b95efda2297a485d8fa4767 to your computer and use it in GitHub Desktop.
Refactoring and abstracting a method to a module for use between multiple classes
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
module GenericHelpers | |
def make_hash_by_attribute(array, attribute) | |
array.each_with_object({}) do |array_item, new_hash| | |
new_hash[array_item.send("#{attribute}")].nil? ? new_hash[array_item.send("#{attribute}")] = 1 : new_hash[array_item.send("#{attribute}")] += 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment