On Apr 29, 2007, at 9:38 AM, David A. Black wrote:

> 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 :-)

Well, not yet:

$ ruby_yarv -e 'p %w[c bb aaa].max_by { |e| e.to_s.size }'
"aaa"

James Edward Gray II