On 4/30/07, Robert Klemme <shortcutter / googlemail.com> wrote:

>
> I am sorry, I did not see it.
I was just kidding Robert.
>
> > 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

>
> Hm, I'd probably use case here.  Let's see...
>
> def longest
>    inject([]) do |lg, e|
>      case
>        when lg.empty?, lg.first.size == e.size
>          lg << e
>        when lg.first.size < e.size
>          [e]
>        else
>          lg
>      end
>    end
> end
better already, I am still looking for *the* solution
>
> Alternative, but with higher memory consumption
>
> def longest
>    lg = Hash.new {|h,k| h[k] = []}
>    each {|x| lg[x.size] << x}
>    lg.sort_by {|k,v| k}.last.pop
> end
good idea but I wanted inject :)
> <snip>

Robert
-- 
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw