Hi --

On 4/29/07, Robert Dober <robert.dober / gmail.com> wrote:
> On 4/29/07, David A. Black <dblack / wobblini.net> wrote:
> > You're doing too much work -- let Ruby do it :-)
> ... for sure, but
> >
> >   class Array
> >     def longest
> >       map {|e| e.to_s }.max
> >     end
> >   end
> I think it is not exactly what OP wanted
> because %w{x aaaa}.max is "x"
> so I will try to comply to his needs
> def longest
>     map{ |e| [e.to_s.size, e.to_s]}.max.first
> end

You're right that I was wrong (because I didn't do the .size thing),
but yours gives you an integer:

  ['x', 'aaaa'].longest  # => 4

I think you'd have to reverse your array. See Harry's code too, which
uses a block with max.  You could also do:

     sort_by {|e| e.to_s.size }[-1]

(since there's no max_by :-)


David

-- 
Upcoming Rails training by Ruby Power and Light:
  Four-day Intro to Intermediate
  May 8-11, 2007
  Edison, NJ
  http://www.rubypal.com/events/05082007