Logan Capaldo wrote:
> I don't think you should use #compact. What if #a_method happens to 
> return nil as a result? Also rescuing NoMethodError, is risky if you 
> have a bug inside #a_method.
> 
> I would write it:
> 
> module Enumerable
>   def selective_collect(msg_name, *args)
>       inject([]) do |result_list, value|
>         if value.respond_to? msg_name
>            result_list << value.send(msg_name, *args)
>         end
>         result_list
>       end
>   end
> end
> 
> x = [ ... ]
> x.selective_collect(:a_method)
> 
> of x.selective_collect(:another_method, "hello")

Yeah, I made a similar implementation -- it just doesn't feel Rubyish 
enough not to pass a block to an iterator...

Well, I guess it's a border case; a general solution is probably not needed.


Thanks for your response,
Daniel