From: Chris Carter Date: 2007-04-29T23:11:06+09:00 Subject: Re: Array.which_long? ( I coded an extension for Array ) On 4/29/07, Billy Hsu 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 -- > How about: class Array def longest self.map {|x| x.to_s }.sort_by {|x| x.size}[-1] end end -- Chris Carter concentrationstudios.com brynmawrcs.com