Robert Klemme <shortcutter / googlemail.com> wrote:
> Of course there are ways that could be used to implement what you want, e.g.
> 
> module Enumerable
>   # default: new behavior
>   def select_2(cl = self.class)
>     r = (cl.new rescue [])
>     each {|x| r << x if yield x}
>     r
>   end
> end
> 
> But this does not solve the major issue that you change the interface.
>  Currently there is a ton of code around that works with the
> assumption (explicit or implicit) that #select returns an Array.  I
> would not dare judge how much code will be broken if we change the
> default behavior.

This is why I would not want this as _default_ behavior. I would expect
select, reject etc return an Array as it is now. But if the class including
Enumerable provides a _special_ MyCollection::enumNew method it would be
used instead.

So something like
 
module Enumerable
  def select
    r = (self.class.enumNew(self) rescue [])
    # [ rest of the select code ]
  end
end

would do. This should not break old code.
I am not sure how to implement this best, as creating an empty collection
and adding elements by << ma be not the most efficient way to go.

(please note that MyCollection.enumNew takes the original object as
 parameter to be able to copy other instance variables etc)

kind regards,

Klaus

-- 
http://lapiz.istik.de/

The Answer is 42. And I am the Answer. Now I am looking for the Question.