# Enumerable#collect and #select best practices

**URL:** <https://www.ruby-forum.com/t/enumerable-collect-and-select-best-practices/60693>\
**Category:** Ruby\
**Created:** [May 16, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/enumerable-collect-and-select-best-practices/60693 "2006-05-16T20:19:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [May 16, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/enumerable-collect-and-select-best-practices/60693/1 "2006-05-16T20:19:48Z")

</div>

I need to collect the results of calling a method on each object in an  
Enumerable that defines that method. Currently, I have this:

enum.collect{|obj| obj.a\_method rescue nil }.compact

though this could also work:

enum.select do |obj|  
obj.respond\_to? :a\_method  
end.collect{|obj| obj.a\_method }

although that’s a bit verbose.

Is there any best practices I should follow?

Daniel

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [May 16, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/enumerable-collect-and-select-best-practices/60693/2 "2006-05-16T20:23:20Z")

</div>

Daniel S. wrote:

> enum.collect{|obj| obj.a\_method rescue nil }.compact

@#%&@½£@!!!

The simple version didn’t work (only in my head), so I’m back at the  
verbose version…

Daniel
