Skip to content

Instantly share code, notes, and snippets.

@dsasse07
Created December 28, 2020 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsasse07/4680d84d5b95efda2297a485d8fa4767 to your computer and use it in GitHub Desktop.
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
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