------art_41282_2060570.1168603057911
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 1/12/07, Alexandru E. Ungur <alexandru / globalterrasoft.ro> wrote:
>
> >>> sender: "Robert Dober" date: "Fri, Jan 12, 2007 at 06:56:08PM +0900"
> <<<EOQ
> > [..]
> >
> > The following is a very common pattern.
> >
> >    an_enum.map{ |ele| ele.a_method }
> >
> > I believe that it is sufficiently common to be generalized.
> >
> > There are two idioms I would like to see
> >
> >   an_enum.apply_to_all :a_method
>
> Ok, I'm still a newbie, but why not extend map instead of introducing a
> new method? As in:


Sure a possibility, I personally like it - even with the magic dot notation
(map.capitalize is also a possibility)
if I recall correctly the map extension idea was rather badly seen the last
time, so I did not think about it anymore.
Thank you for the input.

    %w[apples oranges kiwi].map :capitalize  # ["Apples", "Oranges",
> "Kiwi"]
>
> Still feels like the good old "map" and acts like it...
>
> Here's my naive implementation, that produced the result above:
>
> class Array
>   alias :old_map :map
>
>   def map(arg  il, &block)
>     if block
>       old_map &block
>     elsif ! arg.nil?
>       if arg.is_a?(Symbol)
>         old_map {|x| x.send(arg)}
>       else
>         raise ArgumentError, "Don't know how to handle #{arg.class}
> arguments"
>       end
>     else
>       raise ArgumentError, "No parameter and no block given!"
>     end
>   end
> end
>
>
> All the best,
> Alex
>
> Robert


-- 
"The best way to predict the future is to invent it."
- Alan Kay

------art_41282_2060570.1168603057911--