Prev: extending a core class cleanly
Next: [ANN] RubyInstaller: 1.8.7-p299, 1.9.1-p429 and 1.9.2-rc1 installersand packages released
From: w_a_x_man on 13 Jul 2010 11:32 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 13 Jul 2010 12:57
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 }} |