From: Daniel Martin Date: 2007-04-30T21:09:02+09:00 Subject: Re: Array.which_long? ( I coded an extension for Array ) "Robert Dober" writes: > But it was to return an array of all longest elements, I guess you can > maybe refine it, it seems clumsy, so I repost it just if you have some > time to play ;) > > 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 That does seem clumsy. How about: class Array def longest inject([0,[]]) { |(s,l),e| if e.to_s.size == s then [s,l << e] elsif e.to_s.size < s then [s,l] else [e.to_s.size, [e]] end }[1] end end Gotta love those destructuring block parameters. -- s=%q( Daniel Martin -- martin@snowplow.org puts "s=%q(#{s})",s.to_a[1] ) puts "s=%q(#{s})",s.to_a[1]