60

How can you iterate through an array of objects and return the entire object if a certain attribute is correct?

I have the following in my rails app

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

array_of_objects.each { |favor| favor.completed }

but for some reason these two return the same result! I have tried to replace each with collect, map, keep_if as well as !favor.completed instead of favor.completed == false and none of them worked!

Any help is highly appreciated!

5
  • What I want is to return the entire object favor if favor.completed and if !favor.completed
    – fardin
    Commented Jan 30, 2016 at 18:11
  • 1
    the each method always returns the original array. Commented Jan 30, 2016 at 18:11
  • 1
    Just to qualify @sugar's comment, when it has a block, Array#each returns its receiver. Commented Jan 30, 2016 at 19:04
  • What do you mean by, "return the entire object"? Do you mean you want to return an array of those objects from the first array for which a given attribute evaluates true? Or evaluates 'false'? Commented Jan 30, 2016 at 19:11
  • yes that's what I meant. I don't want to have the attribute returned
    – fardin
    Commented Jan 30, 2016 at 21:05

4 Answers 4

77
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.

5
  • Are there any non-destructive methods similar to select? Commented Jan 30, 2016 at 18:30
  • Didn't understand non-destructive, sorry. Commented Jan 30, 2016 at 18:31
  • select changes the original array, I was just wondering if there's a similar specialised method that keeps the original array intact? Commented Jan 30, 2016 at 18:35
  • 4
    No select doesn't change self. select! does. Commented Jan 30, 2016 at 18:45
  • Oh yes, I see now. Got confused now. Commented Jan 30, 2016 at 18:52
15

For first case,

array_of_objects.reject(&:completed)

For second case,

array_of_objects.select(&:completed)
4
3

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

array_of_objects.find_all { |favor| favor.completed == false }
2
  • both find and 'find_all'` are returning nil for me! :( very annoying
    – fardin
    Commented Jan 30, 2016 at 18:33
  • this is the array of objects I'm calling them on [#<RequestedFavor id: 8, title: "Moving day", description: "Help me move to my new flat please!", user_id: 1, completed: false, created_at: "2016-01-30 18:19:33", updated_at: "2016-01-30 18:19:33", type: "RequestedFavor">, #<OfferedFavor id: 9, title: "Got two hours free", description: "I can spare two hours on Friday", user_id: 1, completed: false, created_at: "2016-01-30 18:19:52", updated_at: "2016-01-30 18:19:52", type: "OfferedFavor">]
    – fardin
    Commented Jan 30, 2016 at 18:34
0

For newer versions of ruby, you can use filter method

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

Reference: https://apidock.com/ruby/Array/filter

1
  • "Newest" means Ruby from 1.8.6_287 ?? (by your reference)
    – Cadoiz
    Commented Jul 20, 2023 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.