Ruby find and return objects in an array based on an attribute Ruby find and return objects in an array based on an attribute arrays arrays

Ruby find and return objects in an array based on an attribute


array_of_objects.select { |favor| favor.completed == false }

Will return all the objects that's completed is false.

You can also use find_all instead of select.


For first case,

array_of_objects.reject(&:completed)

For second case,

array_of_objects.select(&:completed)


You need to use Enumerable#find_all to get the all matched objects.

array_of_objects.find_all { |favor| favor.completed == false }