Enumerable#collect and #select best practices


#1

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


#2

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