[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Array.which_long? ( I coded an extension for Array

Harry Kakueki

4/29/2007 2:14:00 PM

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

Hi,

Welcome.

You may be interested in this.
I did not study your code well but I think it is doing something similar.

p [1, 23, '456'].max {|x,y| x.to_s.size <=> y.to_s.size}

Harry

--
http://www.kakueki.com/ruby...
A Look into Japanese Ruby List in English