I think you've moved in the right direction but the second implementation still makes my head hurt. How about something like this?
# Method call items_found_for(volunteers) # Method implementation def items_found_for(volunteers) volunteers.reduce([]) do |items, volunteer| next items unless volunteer.items.any? items << volunteer.items end.flatten.uniq end
(Perhaps also with the flattening and removing of duplicates handled within the reduce block to avoid the intermediate arrays.)
Now there's no need to explicitly return the empty array because items_found_for will return an empty array if there's nothing to add to it.
items_found_for
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think you've moved in the right direction but the second implementation still makes my head hurt. How about something like this?
(Perhaps also with the flattening and removing of duplicates handled within the reduce block to avoid the intermediate arrays.)
Now there's no need to explicitly return the empty array because
items_found_forwill return an empty array if there's nothing to add to it.