Kevin Ballard wrote:
> class EachProxy < Object
>   instance_methods.each { |meth| undef_method(meth) unless meth.to_s =~
> /^__/ }
>
>   def initialize(obj)
>     @obj = obj
>   end
>
>   def method_missing(meth, *args, &block)
>     @obj.each { |item| item.send meth, *args, &block }
>   end
> end
>
> class Array
>   alias :__each__ :each
>   def each(&blk)
>     if blk
>       __each__(&blk)
>     else
>       EachProxy.new(self)
>     end
>   end
> end

BTW, this also makes things like the following possible:

  ary = %w{a list of words goes here}
  ary.each.gsub!(/[aeiou]/, '') # returns the list minus vowels

But of course it's only useful for side-effect functions (like gsub!)
or functions that take blocks (like each).

Perhaps I should extend collect to work similarly as well? Currently
collect with no arguments simply returns the array unchanged, but
returning a similar proxy object might be nicer. It would let you do
things like:

  ary = %w{a list of words goes here}
  ary.collect.length # returns the length of all words