Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Refactoring and abstracting a method to a module for use between multiple classes
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