On Github vaidehijoshi / Refactoring-of-Self
def grouped_items
items_by_category = {}
items = Item.all.collect { |i| [i.category, i.name] }
items.each do |category, item|
if items_by_category[category]
items_by_category[category] << item
else
items_by_category[category] = [item]
end
end
items_by_category
end
end
def grouped_items
Item.all.each_with_object({}) { |i, hash| (hash[i.category] ||= []) << i }
end
def grouped_items
Item.all.group_by(&:category)
end
😻
😿
🙀
code.refactor
😺