On 19/10/05, Pit Capitain <pit / capitain.de> wrote:
> Brian Schröäer schrieb:
> > 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"]
>
> With the famous
>
>    class Symbol
>      def to_proc
>        lambda { |obj| obj.send self }
>      end
>    end
>
> you can get the same result via
>
>    %w{a long list of words}.map(&:length).map(&:to_s)
>
> I'd bet the Symbol#to_proc is somewhere in Nano.
>
> Regards,
> Pit
>
>

Thats interesting pit. Thank you. Though I'm quite shure I'd never use
either of the solutions proposed here, because I think they introduce
unreadability by uncommon magic. Better to write

%w{a long list of words}.map { | e | e.length.to_s }

which additionally has less line noise in it.

regards,

Brian


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

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