From: w_a_x_man on
On Jul 12, 10:20 pm, w_a_x_man <w_a_x_...(a)yahoo.com> wrote:
> On Jul 6, 6:24 am, Martin DeMello <martindeme...(a)gmail.com> wrote:
>
>
>
>
>
> > On Tue, Jul 6, 2010 at 4:59 AM, Intransition <transf...(a)gmail.com> wrote:
>
> > >   def command(argv)
> > >      lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size
> > > <=> a[1].size }
>
> > at the least this should be registry.map {|cc| [cc, cc.cli]}.sort_by
> > {|a| -a[1].size}
>
> > also, you're never using cc, so why carry it around?
>
> > lookup = registry.map {|cc| cc.cli}.sort_by {|cli| -cli.size}
>
> > and using the ever-handy symbol.to_proc
>
> > lookup = registry.map(&:cli).sort_by(&:size).reverse
>
> > >      x = lookup.find do |cc, cli|
> > >        t = true
> > >        cli.each_with_index do |r, i|
> > >          t = t && (r === argv[i])
> > >        end
> > >        t
> > >      end
>
> > # untested
> > lookup.find do |cli|
> >   cli.zip(argv).inject(true) {|e, (i, j)| e && (i === j)}
> > end
>
> lookup.find{|cli|  cli == argv[0,cli.size] }

That won't work if cli contains a regexp.

lookup.find{|cli|  cli.zip(argv).all?{|a,b| a == b }

From: w_a_x_man on
On Jul 13, 10:32 am, w_a_x_man <w_a_x_...(a)yahoo.com> wrote:
> On Jul 12, 10:20 pm, w_a_x_man <w_a_x_...(a)yahoo.com> wrote:
>
>
>
>
>
> > On Jul 6, 6:24 am, Martin DeMello <martindeme...(a)gmail.com> wrote:
>
> > > On Tue, Jul 6, 2010 at 4:59 AM, Intransition <transf...(a)gmail.com> wrote:
>
> > > >   def command(argv)
> > > >      lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size
> > > > <=> a[1].size }
>
> > > at the least this should be registry.map {|cc| [cc, cc.cli]}.sort_by
> > > {|a| -a[1].size}
>
> > > also, you're never using cc, so why carry it around?
>
> > > lookup = registry.map {|cc| cc.cli}.sort_by {|cli| -cli.size}
>
> > > and using the ever-handy symbol.to_proc
>
> > > lookup = registry.map(&:cli).sort_by(&:size).reverse
>
> > > >      x = lookup.find do |cc, cli|
> > > >        t = true
> > > >        cli.each_with_index do |r, i|
> > > >          t = t && (r === argv[i])
> > > >        end
> > > >        t
> > > >      end
>
> > > # untested
> > > lookup.find do |cli|
> > >   cli.zip(argv).inject(true) {|e, (i, j)| e && (i === j)}
> > > end
>
> > lookup.find{|cli|  cli == argv[0,cli.size] }
>
> That won't work if cli contains a regexp.
>
> lookup.find{|cli|  cli.zip(argv).all?{|a,b| a == b }

Should be

lookup.find{|cli|  cli.zip(argv).all?{|a,b| a === b }}