Hi --

On 4/29/07, Billy Hsu <ruby.maillist / gmail.com> wrote:
> Hi, I'm CFC
> I'm new at here.
> Nice to meet you:)

Welcome!

> I just coded an extension for Array.
> It will return the longest element of an array.
> Source:
>
> class Array
>   def which_long?
>     # Version 1.0
>     # Coded by CFC < zusocfc @ gmail . com >
>     # PLEASE DO NOT REMOVE THE COMMENT OF THIS FUNCTION, THANKS A LOT.
>     # Usage:
>     #   ['a', 'ab', 'abc' 1234].which_long?
>     #   => 1234
>     max, long, self_cpy = 0, "", []
>     self.size.times{|i| self_cpy << self[i].to_s}
>     self_cpy.each{|item| (max = item.size; long = item) if item.size > max }
>     long
>   end
> end
>
> Usage:
>   puts [1, 23, '456'].which_long?
>   => 456

You're doing too much work -- let Ruby do it :-)

  class Array
    def longest
      map {|e| e.to_s }.max
    end
  end


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