On Wed, 27 Nov 2002 dblack / candle.superlink.net wrote:

[snip]
D> What I don't like is the idea that #collect and #collect_with_index
D> behave so differently: one being an iterator instance method, the
D> other a kind of utility/batch-iterator.
[snip]


they don't behave differently at all, there are two of them : an instance
method #collect and the class method #collect !

  Array#collect &block                                             :
    iterator instance method mixed in via Enumerable#collect

  Array#each &block                                                :
    iterator instance method mixed in via Enumerable#each

  Array#each_with_index &block                                     :
    iterator instance method mixed in via Enumerable#each_with_index

  Enumerable#Enumerable.collect *enumerables, &block               :
    utility/batch-iterator module method

  Enumerable#Enumerable.each *enumerables, &block                  :
    utility/batch-iterator module method

  Enumerable#Enumerable.each_with_index *enumerables, &block       :
    utility/batch-iterator module method

  Enumerable#Enumerable.collect_with_index *enumerables, &block    :
    utility/batch-iterator module method


the instace method is the one we all know and love

the module method works on multiple instances of things which are Enumerable
and thus already have their own (instance) collect - your batch/utility
method.  it just happens to have the same name since it's the same thing!



it uses this idiom

  class Foo

    def method
      id
    end

    def Foo.method (*foos)
      foos.collect {|foo| foo.method}
    end

  end

which i personally like (and many will hate - i know)


eg.  the class knows best how to apply a method to a bunch it's instances
since perhaps there are efficiency steps which can be taken if the
implementation is known (like in this case).

the class method being named the same as the instance method make sense since
it does exactly the same thing.

it makes sense for the utility/batch method to be in the class since that's
where the instance method is also defined - imho.

where would you put them?

what would you call them?

i would object to new names (like zip) on the basis that every one already
knows what collect, each_with_index, etc. do - and it's already *doccumented*
so the class method would be easy to grasp even without additional docs.  most
ruby novices have a bit of a time grasping the iterator methods - why throw a
bunch of new names at them (or me ;-) )?

[snip]
D> We're not at cross purposes -- that is, I understood what you meant.
D> I just disagree :-)  I think collect_with_index belongs among the
D> instance method of enumerables, just like collect, each, and
D> each_with_index.
[snip]

yes, i agree.  my last post has them there and refers to this?  check out the
source and let me know what you think.

-a

-- 

 ====================================
 | Ara Howard
 | NOAA Forecast Systems Laboratory
 | Information and Technology Services
 | Data Systems Group
 | R/FST 325 Broadway
 | Boulder, CO 80305-3328
 | Email: ahoward / fsl.noaa.gov
 | Phone:  303-497-7238
 | Fax:    303-497-7259
 ====================================