On 19/10/05, Kevin Ballard <kballard / gmail.com> wrote:
> James Edward Gray II wrote:
> > Hmm, isn't what you wrote an automatic Generator class?  (See
> > standard library.)
>
> Not really. Generator basically gives you external iterators which you
> can access element by element, so you can do things like iterate over 2
> enumerables in step. What I wrote is basically just a convenience for
> applying a method call to all the elements in a single enumerable.
> After all,
>
>   %w{a long list of words}.collect.length
>
> is the same thing as
>
>   %w{a long list of words}.collect { |item| item.length}
>
> but easier to read.
>

I'd understand this
>   %w{a long list of words}.collect.length
as giving me the length of the return value of collect. Collect
returns an array so I'd expect the above to return five. Maybe it
would be better to write

%w{a long list of words}.collect_with :length

where in method missing something like this would happen

module Enumerable
  def method_missing(method, *methods, &block)
    if /(^.*)_with$/ =~ method.to_s
      send($1) do | a |	
	methods.inject(a)  { | r, m | r.send(m) }
      end
    else
      super
    end
  end
end

%w{a long list of words}.collect_with :length, :to_s
# => ["1", "4", "4", "2", "5"]

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/