From: Robert Dober Date: 2007-04-30T00:21:18+09:00 Subject: Re: Array.which_long? ( I coded an extension for Array ) On 4/29/07, David A. Black wrote: > Hi -- > > On 4/29/07, Billy Hsu wrote: > > Thanks for your reply, I learned more on this thread :P > > But I have a question: > > If I have an array contain: > > ary = [1, 12, 234, "456"] > > there has two elements which size is 3, but the longest method just returned > > one of them. > > I can't solve it :( Alternatively I can use my erroneous code from above to give you all longest elements def longest longest_size = map{ |e| [e.to_s.size, e.to_s]}.max.first reject{ |x| x.size < longest} end this is slow though, maybe the following is preferable - but not so readable - def longest inject([]){ |s, e| if s.empty? || s.first.size < e.to_s.size then [e] elsif s.first.size == e.to_s.size then s << e else s end } end > Robert You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw